【PAT】1071. Speech Patterns (25)【map容器的使用】
題目描述
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?
翻譯:對於同一個單詞的同義詞,每個人都有一種偏愛。舉個例子,一些人喜歡用”the police”, 但是另外的人可能更喜歡用”the cops”。通過模仿這種習慣可以幫助縮小演講者的身份,舉個例子,當需要證實網路背後是否仍是同一個人的時候很有用。
INPUT FORMAT
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].
翻譯:每個輸入檔案包含一組測試資料。對於每組輸入資料,包括一行不超過1048576個字元長度的文字,直到 ‘\n’時結束。輸入至少包含一個字母或數字,即在[0-9 A-Z a-z]範圍內的字元。
OUTPUT FORMAT
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
解題思路
先根據題目要求分割字串,注意只有a-z,A-Z 和0-9的字串是單詞,其他都是分隔符。如果字母為大小字母需要轉換為小寫字母儲存。然後將得到的每個單詞儲存到map容器中,如果Max小於當前單詞的個數或等於當前單詞個數但字串S字典序大於當前單詞就更新。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<map>
#include<algorithm>
#define INF 99999999
using namespace std;
map<string,int> mp;
int Max=0;
string S;
char s[1048580];
int main(){
gets(s);
int length=strlen(s);
string a;
int flag=0;
for(int i=0;i<length;i++){
if((s[i]>='a'&&s[i]<='z')||(s[i]>='A'&&s[i]<='Z')||(s[i]>='0'&&s[i]<='9')){
if(s[i]>='A'&&s[i]<='Z')s[i]=s[i]-'A'+'a';
a.push_back(s[i]);
flag=1;
if(i==length-1){
mp[a]++;
if(Max<mp[a]||Max==mp[a]&&S>a){
Max=mp[a];
S=a;
}
a.clear();
flag=0;
}
}
else if(flag==1){
mp[a]++;
if(Max<mp[a]||Max==mp[a]&&S>a){
Max=mp[a];
S=a;
}
a.clear();
flag=0;
}
}
printf("%s %d\n",S.c_str(),Max);
return 0;
}