题意:统计由数字和字母组成的“个数”,找出出现频率最高的那个。
思路:对一个单词一个单词的扫描进map记数
#include<vector>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#include<set>
#include<map>
#include<cstring>
#include<string>
using namespace std;
bool check(char c)
{
if (c >= '0' && c <= '9')
return true;
if (c >= 'A' && c < 'Z')
return true;
if (c >= 'a' && c <= 'z')
return true;
return false;
}
bool cmp(pair<string,int> p1, pair<string,int> p2)
{
return p1.second > p2.second;
}
int main()
{
map<string, int>count;
string str;
getline(cin, str);
int i = 0;
while (i < str.length())
{
string word;
while (i < str.length() && check(str[i]))
{
if (str[i] >= 'A' && str[i] <= 'Z')
str[i] += 32;
word += str[i];
i++;
}
if (count.find(word) == count.end())
count[word] = 1;
else
count[word]++;
while (i < str.length() && !check(str[i]))
i++;
}
vector<pair<string, int> >vpr;
for (map<string, int>::iterator it = count.begin(); it != count.end(); it++)
{
vpr.push_back(make_pair(it->first, it->second));
}
sort(vpr.begin(), vpr.end(),cmp);
auto it = vpr.begin();
cout << it->first << " " << it->second << '
';
}