PAT(stl)——1071. Speech Patterns (25)

People often have a preference among synonyms of the same word. For example, some may prefer “the police”, while others may prefer “the cops”. Analyzing such patterns can help to narrow down a speaker’s identity, which is useful when validating, for example, whether it’s still the same person behind an online avatar.

Now given a paragraph of text sampled from someone’s speech, can you find the person’s most commonly used word?

Input Specification:

Each input file contains one test case. For each case, there is one line of text no more than 1048576 characters in length, terminated by a carriage return ‘\n’. The input contains at least one alphanumerical character, i.e., one character from the set [0-9 A-Z a-z].

Output Specification:

For each test case, print in one line the most commonly occurring word in the input text, followed by a space and the number of times it has occurred in the input. If there are more than one such words, print the lexicographically smallest one. The word should be printed in all lower case. Here a “word” is defined as a continuous sequence of alphanumerical characters separated by non-alphanumerical characters or the line beginning/end.

Note that words are case insensitive.

Sample Input:
Can1: “Can a can can a can? It can!”
Sample Output:
can 5

题目大意:

计数题。

题目解析:

可以用string作为map的键值,那就好做了。

具体代码:

#include<iostream>
#include<algorithm>
#include<string>
#include<map>
using namespace std;
map<string,int> m;
int main()
{
	string str,tmp;
	getline(cin,str);
	for(int i=0;i<str.size();i++){
		if(isalnum(str[i])){
			if(isupper(str[i]))
				str[i]=tolower(str[i]);
			tmp+=str[i];
		}
		if(!isalnum(str[i])||i==str.size()-1){
			if(!tmp.empty()){
				m[tmp]++;
				tmp.clear();
			}
		}
	}
	int max=0;
	string maxchar;
	for(auto it=m.begin();it!=m.end();it++){
		if(it->second>max){
			max=it->second;
			maxchar=it->first;
		}
	}
	cout<<maxchar<<" "<<max;
    return 0;
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
昨天 17:58
点赞 评论 收藏
分享
06-23 11:28
门头沟学院 Java
牛客91966197...:也有可能是点拒绝的时候自动弹的话术
点赞 评论 收藏
分享
水墨不写bug:疑似没有上过大学
点赞 评论 收藏
分享
感觉他们一点都不了解现在这个社会就业有多难,已经在牛客刷到好多篇&nbsp;延毕的帖子了,延毕就会导致已经找好的工作就没了,还得重新再找,学校和老师们是怎么想的呢????看到学生丢失工作会开心吗&nbsp;就业数据都在造假,真实的就业困难不去解决&nbsp;一个个真是好样的
从明天开始狠狠卷JV...:学生看到的是导师不放实习导致offer黄了。 导师看到的是招进来的学生吃自己补助和自己的招生名额,却没给自己升迁带来任何帮助,还要跑路。 根本利益的不一致,最主要留校的导师大概率是职场上招聘失败的,被迫留校的,什么牛鬼蛇神都会有
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务