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
#include<iostream>
#include<map>
#include<string>
using namespace std;
bool check(char c){
if(c >= '' && c <= '') return true;
if(c >= 'a' && c <= 'z') return true;
if(c >= 'A' && c <= 'Z') return true;
return false;
}
int main(){
map<string,int> count;
string str;
getline(cin,str);
int i = ;
while(i < str.length()){
string word;
while(i < str.length() && check(str[i]) == true){
if(str[i] >= 'A' && str[i] <='Z'){
str[i] += ;
}
word += str[i];
i++;
}
if(word != " "){
if(count.find(word)!=count.end()) count[word]++;
else count[word] = ;
}
while(i < str.length() && check(str[i]) == false){
i++;
}
}
string ans;
int max = ;
for(map<string,int>::iterator it = count.begin(); it!=count.end();it++){
if(it -> second > max){
ans = it -> first;
max = it -> second;
}
}
cout << ans << " " << max << endl;
return ;
}

20200104

最后一个测试点没通过,应该是空符的问题,待排查

#include<iostream>
#include<string>
#include<map>
using namespace std; bool check(char c); int main()
{
string str;
string word;
map<string, int> mp; getline(cin, str); int len = str.length(); for (int i = ; i < len; i++)
{
if (check(str[i]))
{
if (str[i] >= 'A' && str[i] <= 'Z')
{
str[i] += ;
}
word += str[i];
continue;
}
else if (word != "" && (!word.empty()))
{
if (mp.find(word) != mp.end())
{
mp[word]++;
}
else
{
mp[word] = ;
}
word.clear();
}
} string ans;
int max = ;
for (auto it = mp.begin(); it != mp.end(); it++)
{
if (it->second > max && it->first != "")
{
ans = it->first;
max = it->second;
}
} cout << ans << " " << max << endl;
return ;
} bool check(char c)
{
if (c >= '' && c <= '')
{
return true;
}
else if (c >= 'a' && c <= 'z')
{
return true;
}
else if (c >= 'A' && c <= 'Z')
{
return true;
} return false;
}

1071 Speech Patterns (25)(25 分)的更多相关文章

  1. PAT 1071 Speech Patterns[一般]

    1071 Speech Patterns (25 分) People often have a preference among synonyms of the same word. For exam ...

  2. PAT 甲级 1071 Speech Patterns (25 分)(map)

    1071 Speech Patterns (25 分)   People often have a preference among synonyms of the same word. For ex ...

  3. 1071 Speech Patterns——PAT甲级真题

    1071 Speech Patterns People often have a preference among synonyms of the same word. For example, so ...

  4. PAT Advanced 1071 Speech Patterns (25 分)

    People often have a preference among synonyms of the same word. For example, some may prefer "t ...

  5. 【PAT甲级】1071 Speech Patterns (25 分)(getline(cin,x))

    题意: 输入一行字符串,输出出现过次数最多的由字母和数字组成的字符串以及它出现的次数(对大小写不敏感,输出全部输出小写). AAAAAccepted code: #define HAVE_STRUCT ...

  6. 1071. Speech Patterns (25)

    People often have a preference among synonyms of the same word. For example, some may prefer "t ...

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

    简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

  8. PAT甲题题解-1071. Speech Patterns (25)-找出现最多的单词

    分割字符串的用法+map映射给出input中出现次数最多的单词,如果次数相同,给出按字典序最小的. 这里我用了自定义分隔符来读取字符串,方法如下: //按照定义的分隔符d来分割字符串,对str进行读取 ...

  9. 1071 Speech Patterns

    People often have a preference among synonyms of the same word. For example, some may prefer "t ...

随机推荐

  1. 支持移动端裁剪图片插件Jcrop(结合WebUploader上传)

    (此教程包括前端实现图片裁剪,后端进行获取裁剪区并保存) 最近有一个需求,微信公众号上传图片,支持自定义裁剪. 以前用过一款裁剪插件cropper,很久没用了,不知道对移动端操作兼容如何,重新从网上搜 ...

  2. easyui-datagrid清空表中原有数据

    $('#dg').datagrid('loadData', { total: 0, rows: [] });

  3. 用PHP写PHP7扩展,超级简单对吧!

    [图片打不开,请用代理] 介绍: PHP扩展是编译库,它允许在您的PHP代码中使用特定的功能(主要是使用C编写的php扩展). 例如,您需要使用PHP使用SQLite3,您可以实现自己的方法和功能来连 ...

  4. c#自制抽奖小程序

    #region 第一部分界面设计 ; Button button = new Button(); Image[] images = new Image[N]; PictureBox[] picture ...

  5. 【洛谷 P5341】 [TJOI2019]甲苯先生和大中锋的字符串(后缀自动机)

    题目链接 建出\(sam\),求出parent tree上每个点的\(endpos\)集合大小. 如果等于\(k\),说明到达这个点的都可以.给\((len[fa(i)],len[i]]\)的\(cn ...

  6. asBroadcastStream

    StreamSubscription sc = StreamSubscription(); Stream s = Stream(); sc.addStream(s); var bs = sc.stre ...

  7. iOS - 小功能 跳转到淘宝或天猫的商品展示详情页

    最近做iOS项目的时候遇到一个需求,点击广告页,跳转到淘宝或天猫的商品详情页. 具体需要是这样: 1)安装了淘宝:跳转到淘宝详情页. 2)没装淘宝,装了天猫:跳转到天猫详情页 3)淘宝.天猫都没装:跳 ...

  8. it commit提示Your branch is up-to-date with 'origin/master'.

    今天提交git仓库的时候,遇到了如截图所示的问题,提示Your branch is up-to-date with 'origin/master'. 查了些资料后,发现其根本原因是版本分支的问题 这时 ...

  9. js 一些有意思的小Demo

  10. IOS 之 NSBundle 使用

    来源:http://blog.sina.com.cn/s/blog_b0c59541010151rd.html An NSBundle object represents a location in ...