A1071 Speech Patterns (25 分)细节很多

#include
#include
#include
using namespace std;
mapmp;
bool cmp(string a, string b)
{
	if (mp[a] != mp[b]) return mp[a] < mp[b];
	else return a < b;
}
bool flag(char &a)
{
	if (a >= '0'&&a <= '9') return true;
	else if (a >= 'a'&&a <= 'z') return true;
	else if (a >= 'A'&&a <= 'Z')
	{
		a = a - 'A' + 'a';
		return true;
	}
	else return false;
}
int main()
{
	string str;
	string temp;
	getline(cin, str);
	for (int i = 0; i <= str.length(); i++)//注意这里要取到等号,否则123 123 1 123这样的就无法通过,最后一个测试点就是这个,会扣两分。
	{
		if (flag(str[i])) temp += str[i];
		else if (temp != "")//注意这里要限制非空,否则出错
		{
			if (mp.find(temp) == mp.end()) mp[temp] = 0;
			mp[temp]++;
			temp.clear();
		}

	}
	string result;
	int max = -1;
	//for (map::iterator it = mp.begin(); it != mp.end(); it++)
	//cout << it->first << " " << it->second << endl;
	for (map::iterator it = mp.begin(); it != mp.end(); it++)//map内部自动按字典序排序
	{
		if (it->second > max)
		{
			max = it->second;
			result = it->first;
		}
	}

	cout << result << " " << max << endl;
	return 0;
}

你可能感兴趣的:(A1071 Speech Patterns (25 分)细节很多)