[英]First most repeated word based on insertion order
我有一堆单词按顺序插入字符串中。 现在,我想找到最重复的单词。 如果有平局,请使用前面的单词。
代码: https : //ideone.com/S5yOBk
#include <iostream>
#include <map>
#include <sstream>
using namespace std;
int main()
{
string line = "paper block book chair";
map<string, int> frequencyMap;
istringstream stream(line);
string word;
while (stream >> word)
{
++frequencyMap[word];
}
string maxRep; int c = 0;
for (auto& t : frequencyMap) {
if (t.second > c) {
maxRep = t.first;
c = t.second;
}
}
cout << "First Most repeated word is: " << maxRep << endl;
}
在这种情况下,预期输出为“纸张”,但我一直处于“阻塞”状态。
您无法根据将元素插入地图的顺序来决定哪个元素将首先出现在地图中。 您应该考虑使用另一个字段来知道哪个将保留插入顺序。
有一个简单的解决方案是这样的:
#include <iostream>
#include <map>
#include <sstream>
using namespace std;
int main()
{
string line = "paper block book chair";
map<string, pair<int,int>> frequencyMap;
istringstream stream(line);
string word;
int i = 0;
while (stream >> word)
{
if(frequencyMap.count(word)==0)
frequencyMap[word]=pair<int,int>(i++,1);
else
frequencyMap[word].second++;
}
string maxRep; int c = 0,ord=10000000;// max elements inserted+1
for (auto& t : frequencyMap) {
if (t.second.second > c ||(t.second.second==c && t.second.first < ord)) {
maxRep = t.first;
ord = t.second.first;
c = t.second.second;
}
}
cout << "First Most repeated word is: " << maxRep << endl;
}
Output:
paper
std :: map是基于某种BST的,它存储的每个第一类都应具有“ opetator <”以使BST排序。 std :: string是按字典顺序排序的。
您可以编写一个包含字符串及其插入时间的新类,并通过插入时间比较每两个元素,以便按插入时间对std :: map进行排序。
UPD
我重新考虑了这个问题,发现您只需要记录每个字符串出现的时间并保持最大时间即可。 如果某个字符串出现的时间大于我们之前记录的最大时间,我们将更新最大时间并将答案更改为该字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.