1. 程式人生 > >PAT-A1071. Speech Patterns (25)

PAT-A1071. Speech Patterns (25)

map應用。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <string> 
#include <algorithm>
using namespace std;

int isLegal(char c)
{
	if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))
		return 1;
	return 0;
}

int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
	string str;
	map<string, int> mp;
	getline(cin, str);
	int i = 0;
	while (i < str.length())
	{
		string word;
		while (i < str.length() && isLegal(str[i]))
		{
			if (str[i] >= 'A' && str[i] <= 'Z')
				str[i] += 'a' - 'A';
			word += str[i];
			i++;
		}
		if (word != "")
			mp[word]++;
		while (i < str.length() && !isLegal(str[i]))
		{
			i++;
		}
	}
	
	string res;
	int mmax = 0;
	for (map<string,int>::iterator it = mp.begin(); it != mp.end(); it++)
	{
		if (it->second > mmax)
		{
			mmax = it->second;
			res = it->first;
		}
	}
	cout << res << " " << mmax;
	return 0;
}