PAT甲级1071
虽然建号很久但还是第一次发文,记录下PAT遇到的一些坑
原题:
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
题目大意是找出一行里出现次数最多的单词
注意点:
1、题目一开始的叙述跟题目要求没有半点关系,:"前面的并不是人名!并不是找某个人说话的口头禅,只是单纯找单词罢了
2、题目也不是以空格为分隔,而是以非有效字符为分隔点,测试点2楼主测试了好久,像是ca;’,./!#n这种只能截取前面的ca或者后面的n,并不能作为can看待(楼主一开始以为是去掉所有非有效字符剩下的当做一个单词)
3、出现最多的单词可以边输入边处理,不用到最后再遍历一遍map;若留到最后查找找到第一个最大值就行了,map键是以字典序为顺序
4、循环到最后要单独处理
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main(){
string s,word;
getline(cin,s);
map<string,int> cnt;
int max=0,index=0;
string tmp;
while(index<s.length()){ //以非有效字符为分界统计单词出现次数
if(isalnum(s[index])) s[index]=tolower(s[index]),tmp+=s[index];
if(!isalnum(s[index])&&tmp.length()!=0){
cnt[tmp]++;
if(max<cnt[tmp]) max=cnt[tmp],word=tmp;
else if(max==cnt[tmp]) if(tmp<word) word=tmp;
tmp="";
}
index++;
}
if(tmp.length()!=0){
cnt[tmp]++;
if(max<cnt[tmp]) max=cnt[tmp],word=tmp;
else if(max==cnt[tmp]) if(tmp<word) word=tmp;
}
cout<<word<<" "<<max;
system("pause");
return 0;
}