pat1071. Speech Patterns (25)

1071. Speech Patterns (25)

时间限制

300 ms

内存限制

65536 kB

代码长度限制

16000 B

判题程序

Standard

作者

HOU, Qiming

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 #include<cstdio>
 2 #include<stack>
 3 #include<algorithm>
 4 #include<iostream>
 5 #include<stack>
 6 #include<set>
 7 #include<map>
 8 using namespace std;
 9 map<string,int> ha;
10 bool ischar(char &a){
11     if(a>=‘A‘&&a<=‘Z‘){
12         a=a-‘A‘+‘a‘;
13         return true;
14     }
15     if((a>=‘a‘&&a<=‘z‘)||(a>=‘0‘&&a<=‘9‘)){
16         return true;
17     }
18     return false;
19 }
20 int main(){
21     //freopen("D:\\INPUT.txt","r",stdin);
22     string s,ss;
23     getline(cin,s);
24
25     //cout<<s<<endl;
26
27     int i;
28     for(i=0;i<s.length();i++){
29         ss="";
30         //cout<<i<<" "<<s[i]<<endl;
31         while(i<s.length()&&ischar(s[i])){
32             ss=ss+s[i];
33             i++;
34         }
35         if(ss!=""){
36             ha[ss]++;
37             //cout<<ss<<" "<<ha[ss]<<endl;
38         }
39
40     }
41     map<string,int>::iterator it;
42     int maxcount=0;
43     for(it=ha.begin();it!=ha.end();it++){
44         if(maxcount<it->second){
45             maxcount=it->second;
46             ss=it->first;
47         }
48     }
49     cout<<ss<<" ";
50     printf("%d\n",maxcount);
51     return 0;
52 }

时间: 2024-11-04 00:47:38

pat1071. Speech Patterns (25)的相关文章

PAT 1071. Speech Patterns (25)

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 ident

A1071. 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 v

1071. Speech Patterns (25)

map的使用 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HOU, Qiming 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

PAT:1071. Speech Patterns (25) AC

#include<ctype.h> #include<iostream> #include<map> #include<string> using namespace std; bool check(char a) //判断是否为有效字符 { if(isdigit(a) || isalpha(a)) return true; return false; } int main() { map<string,int> count; //单词与次数的映

PAT (Advanced Level) 1071. Speech Patterns (25)

简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<iostream> #include<algorithm> using namespace std;

C++ STL map A1071 Speech Patterns(25) (注意如何从字符串里 分割出单词,注意读取整行带空格的string 需要使用 getlint(cin,str) 函数)

#include <bits/stdc++.h> #include<math.h> #include <string> using namespace std; const int maxn = 40010;//最大学生人数 bool check(char c){ if(c >= '0' && c<= '9') return true; if(c >= 'A' && c<= 'Z') return true; if

PAT 1071 Speech Patterns[一般]

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 iden

Design Patterns 25

尽管将一个系统分割成许多对象通常可以增加其可服用性, 但是对象间相互连接的激增又会降低其可复用性了. 大量的连接使得一个对象不可能在没有改变其他对象的支持下工作, 系统表现为一个不可分割的整体, 所以, 对系统的行为进行任何较大的改动就十分困难了. 中介者模式, 用一个中介对象来封装一系列的对象交互. 中介者使各对象不需要显式的相互引用, 从而使其耦合松散, 而且可以独立的改变他们之间的交互. 123456789101112131415161718192021222324252627282930

Speech Patterns (string)

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 v