PATA1071 1071 Speech Patterns (代码与《算法笔记》A1054相似,思路一致)

    技术2022-07-11  83

    #include <iostream> #include <map> #include <string> using namespace std; int main() { string s; getline(cin, s);//先读入整个字符串 map<string, int>count; int i; string str; for (i = 0; i <= s.length(); i++)//这里取“=”保证只输入一个字符时,还可以循环一次,进行判断 { if (s[i] <= '9'&&s[i] >= '0' || s[i] <= 'z'&&s[i] >= 'a')//如果在a-z或0-9,则赋值给判断单词字符串 str += s[i]; else if (s[i] <= 'Z'&&s[i] >= 'A')//如果在A-Z,则转换为小写后赋值 str += s[i] - 'A' + 'a'; else//遇到其他字符,则开始本次判断 { if (str.length() == 0)continue;//跳过其他字符 if (count.find(str) != count.end()) count[str]++; else count[str] = 1; str.erase(str.begin(), str.end()); //清空临时字符串 } } string maxindex; int max = -1; for (map<string, int>::iterator it = count.begin(); it != count.end(); it++) { if (it->second > max) { maxindex = it->first; max = it->second; } } cout << maxindex << " " << max; system("pause"); return 0; }
    Processed: 0.011, SQL: 9