简体   繁体   English

根据插入顺序排列的第一个重复次数最高的单词

[英]First most repeated word based on insertion order

I have a bunch of words inserted in a string in order. 我有一堆单词按顺序插入字符串中。 Now, i'd like to find the most repeated word. 现在,我想找到最重复的单词。 If there is a tie, use the earlier word. 如果有平局,请使用前面的单词。

Code: https://ideone.com/S5yOBk 代码: 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;
}

Expected output is "paper" in this case, but i keep getting "block". 在这种情况下,预期输出为“纸张”,但我一直处于“阻塞”状态。

You can't decide which element will appear first in map based on the order on which you insert them in the map. 您无法根据将元素插入地图的顺序来决定哪个元素将首先出现在地图中。 You should consider using another field to know which will keep the insertion order. 您应该考虑使用另一个字段来知道哪个将保留插入顺序。

There is an easy solution like this:- 有一个简单的解决方案是这样的:

#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 is base on a sort of BST, every first class it stores should have “opetator <“ to make BST ordered. std :: map是基于某种BST的,它存储的每个第一类都应具有“ opetator <”以使BST排序。 std::string is ordered lexicographicaly. std :: string是按字典顺序排序的。

You can write a new class contains the string and the time it insert in, compare every two elements by their insert-time,so the std::map is ordered by inserting time. 您可以编写一个包含字符串及其插入时间的新类,并通过插入时间比较每两个元素,以便按插入时间对std :: map进行排序。

upd UPD

I rethink this problem and find that you just need to is record the times every string occurs and maintain the maximum times. 我重新考虑了这个问题,发现您只需要记录每个字符串出现的时间并保持最大时间即可。 If some string occurs time is greater than the maximum times we recorded previously, we update the maximum times and change the answer into this string. 如果某个字符串出现的时间大于我们之前记录的最大时间,我们将更新最大时间并将答案更改为该字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM