我需要一个提示来解决这个问题 [英] I need a tip to solve this problem
本文介绍了我需要一个提示来解决这个问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
/*Write either a C++ function int triplets(stack<string> words) or a Java method public static
* int triplets(Stack<String> words) that take as the argument a stack of strings and count how
* many elements are included in the stack exactly three times. You should use objects of type
* set<string> in C++ or TreeSet<String> in Java. The set operations are given below (refer to
* Problem 1 for the stack operations):
*/
#include <iostream>
#include <stack>
#include <set>
#include <string>
using namespace std;
string popped="";
int triplets(stack<string>words)
{
set<string>set1{"wow", "AUA", "wow", "AUA", "Data Structures", "AUA", "OOP", "wow"} ;
for(const auto &count: set1)
{
words.push(count);
}
for(int i=0; i<words.size(); i++)
{
for(int current=i; current<words.size(); current++)
{
popped=words.top();
}
}
}
什么我试过了:
我尝试用排队比较前后来解决它但是更难/
What I have tried:
I tried to solve it with queue comparing front to rear but it was more difficult/
推荐答案
您可以先将堆栈的所有字符串插入到集合中。然后创建两个嵌套循环:在外部迭代中设置项目,在堆栈项目内部,计算匹配。
You could first insert all the strings of the stack into the set. Then make two nested loops: in the outer iterate over the set items, in the inner over the stack items, counting the matches.
这篇关于我需要一个提示来解决这个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文