1071 Speech Patterns (25 point(s))

1071 Speech Patterns (25 point(s))

 题解

isalnum 判断字符变量c是否为字母或数字,若是则返回非零,否则返回零。

tolower是一种函数,功能是把字母字符转换成小写,非字母字符不做出处理。

#include<iostream>
#include<map>
#include<cctype>
using namespace std;
int main() {
	string t;
	getline(cin, t);
	map<string, int> res;
	string s;
	for(int i = 0; i < t.length(); ++i) {
		if(isalnum(t[i])) {
			t[i] = tolower(t[i]);
			s += t[i];
		}
		if(!isalnum(t[i]) || i == t.length() - 1) { // 注意最后一个可能是字母。
			if(s.length()) res[s]++;
			s = ""; 
		}
	} 
	int maxn = 0;
	for(map<string, int>::iterator it = res.begin(); it != res.end(); ++it) if(it->second > maxn) {
		maxn = it->second;
		s = it->first;
	}
	cout << s << " " << maxn << '\n';
	return 0;
}