PATA1071 1071 Speech Patterns (代码与《算法笔记》A1054相似,思路一致)

#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
	string s;
	getline(cin, s);//先读入整个字符串
	map<string, int>count;
	int i;
	string str;
	for (i = 0; i <= s.length(); i++)//这里取“=”保证只输入一个字符时,还可以循环一次,进行判断
	{
		if (s[i] <= '9'&&s[i] >= '0' || s[i] <= 'z'&&s[i] >= 'a')//如果在a-z或0-9,则赋值给判断单词字符串
			str += s[i];
		else if (s[i] <= 'Z'&&s[i] >= 'A')//如果在A-Z,则转换为小写后赋值
			str += s[i] - 'A' + 'a';
		else//遇到其他字符,则开始本次判断
		{
			if (str.length() == 0)continue;//跳过其他字符
			if (count.find(str) != count.end())
				count[str]++;
			else
				count[str] = 1;
			str.erase(str.begin(), str.end());	//清空临时字符串	
		}
	}
	string maxindex;
	int max = -1;
	for (map<string, int>::iterator it = count.begin(); it != count.end(); it++)
	{
		if (it->second > max)
		{
			maxindex = it->first;
			max = it->second;
		}
	}
	cout << maxindex << " " << max;
	system("pause");
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值