A1071 Speech Patterns (25 分)细节很多

#include<iostream>
#include<map>
#include<string>
using namespace std;
map<string, int>mp;
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<string, int>::iterator it = mp.begin(); it != mp.end(); it++)
	//cout << it->first << " " << it->second << endl;
	for (map<string, int>::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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值