PAT A1071 Speech Patterns(***从字符串切割单词+map)

问题链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805398257647616

题意:

    令"单词"的定义为大小写字母,数字的组合。给出一个字符串,问出现次数最多的单词及其出现次数(一起除大小写字母和数字外的字符都作为分隔符)。其中字母不区分大小写,且最后按小写字母输出。

Note:

1.读入整行字符串getline(cin, str)

2. 大写字母转小写字符在原字符上+32

#include<cstdio>
#include<iostream>
#include<cstring>
#include<map>
using namespace std;
bool check(char s){
	if(s >= 'a' && s <= 'z')
	    return true;
	if(s >= 'A' && s <= 'Z')
	    return true;
	if(s >= '0' && s <= '9')
	    return true;
	return false;
}
int main(){
	string str;
	getline(cin, str);//读入一整行
	
	map<string, int> mp;
	
	int i = 0;
	while(i < str.length()){
		string word;
		while(i < str.length() && check(str[i]) == true){
			if(str[i] >= 'A' && str[i] <= 'Z')
			    str[i] += 32;
			word += str[i];
			i++;
		}
		if(word != ""){
			if(mp.find(word) != mp.end())
				mp[word] ++;
			else
				mp[word] = 1;
		}
		while(i < str.length() && check(str[i]) == false)
		    i++;
	}
	
	string MAX;
	int MAXK = 0;
	for(map<string, int>::iterator it = mp.begin(); it != mp.end(); it++){
		if(it->second > MAXK){
			MAX = it->first;
			MAXK = it->second;
		}
	}
	
	cout << MAX <<" "<< MAXK;
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值