Enter an article to output the highest-frequency and sub-high-frequency words (all lowercase, comma delimited). There are only three separators for spaces, commas, and periods in the article.
Regardless of the frequency of the same situation. Case is ignored.
input: I am a student. I come from Xidian,i love Xidian.
Output: I,xidian
#include <iostream> #include <string> #include <map> #include <cctype>using namespace Std;int Main (int argc, char *argv[]) {string S; map<string,int>m; while (Getline (cin,s)!=null) {string T; for (int i=0;i<s.size (); ++i) {if (s[i]== ' | | | s[i]== ', ' | | s[i]== '. ') {m[t]+=1; T= ""; } else t+=s[i]; }} int first=0,second=0; String s1,s2; For (Map<string,int>::iterator It=m.begin (); It!=m.end (); ++it) {if (It->second>first) { s1=it->first; first=it->second; } if (It->second>second&&it->second<first) {s2=it->first; second=it->second; }} for (int i=0;i<s1.size (); ++i) {cout<< (char) ToLower (S1[i]); } cout<< ","; for (int i=0;i<s2.size (); ++i) {cout<< (char) ToLower (s2[i]); } cout<< "\ n"; return 0;}
Huawei Machine Test-word count