1071 Speech Patterns 发表于 2020-05-31 输出出现最多的那个字符串(字母和数字的组合),不区分大小写。因为输入中可能有空格,因此用getline(cin, s);输入。空字符串的个数不应该被统计,因此t长度不为0。 12345678910111213141516171819202122232425262728293031#include <iostream>#include <cctype>#include <map>using namespace std;int main() { string s, t; map<string, int> m; getline(cin, s); for (int i = 0; i < s.length(); i++) { if (isalnum(s[i])) { s[i] = tolower(s[i]); t += s[i]; } if (!isalnum(s[i]) || i == s.length() - 1) { if (t.length() != 0) m[t]++; t = ""; } } int resultMax = 0; string resultStr = ""; for (auto it = m.begin(); it != m.end(); it++) { if (it->second > resultMax) { resultMax = it->second; resultStr = it->first; } } cout << resultStr << " " << resultMax << endl; return 0;}