1.首先是字符串处理,把大写字母的全改为小写字母
2.字符串分隔,遇到不是数字或者字母的字符,就进行分隔
3.使用map来存储次数,最后再存到vector中排序输出
4.需要使用scanf读取字符,不能使用getline
AC代码:
//#include<string>
//#include<stack>
//#include<unordered_set>
//#include <sstream>
//#include "func.h"
//#include <list>
#include <iomanip>
#include<unordered_map>
#include<set>
#include<queue>
#include<map>
#include<vector>
#include <algorithm>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
#include<limits.h>
#include<stack>
using namespace std;
/*
Here a "word is defined as a continuous sequence of alphanumerical characters separated by non-alphanumerical characters or the line beginning/end
*/
bool cmp(const pair<string, int>&a,const pair<string, int>&b)
{
if (a.second > b.second) return true;
else if (a.second == b.second && a.first < b.first) return true;
else return false;
}
int main(void)
{
string s="";
char c = '1';
while (c!='\n')
{
scanf("%c", &c);
s += c;
}
map<string, int> times;
string word = "";
for (int i = 0; i < s.size(); i++)
{
if ((s[i] >= 'a'&&s[i] <= 'z') || (s[i] >= 'A'&&s[i] <= 'Z') || (s[i] >= '0'&&s[i] <= '9'))
{//是字母
if ((s[i] >= 'A'&&s[i] <= 'Z'))
s[i] = s[i] - 'A' + 'a';
word += s[i];
}
else
{//不是字母,则清空word
if (word != "")
times[word]++;
word = "";
}
}
vector<pair<string, int>> ans(0);
for (map<string, int>::iterator ite = times.begin(); ite != times.end(); ite++)
{
ans.push_back( *ite);
}
sort(ans.begin(), ans.end(), cmp);
cout << ans[0].first << " " << ans[0].second << endl;
return 0;
}