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;
}

最新文章

  1. mysql 5.7 docker 主从复制架构搭建
  2. Total Commander 8.52 Beta 1
  3. numberOfRowsInSection方法什么时候调用
  4. 翻译:打造基于Sublime Text 3的全能python开发环境
  5. Google加强版权保护
  6. 全球2/3的DNS瘫痪 顶级域名根服务器故障
  7. Android-ViewPagerIndicator框架使用——TabPageIndicator以及样式的修改
  8. 结构体struct和typedef后面接指针的含义
  9. makefile-0711-168 SEVERE ERROR: Input file:
  10. LDAP禁止匿名访问
  11. (原)android的JNI中使用C++的类
  12. 对LevelDB的“升级版”存储引擎RocksDB的调研成果
  13. 百度富文本编辑器ueditor在jsp中的使用(ssm框架中的应用)
  14. [Swift]LeetCode953. 验证外星语词典 | Verifying an Alien Dictionary
  15. 华为oj之字符串分割
  16. [转帖]50个必知的Linux命令技巧,你都掌握了吗?
  17. js高级---本地对象、内置对象、宿主对象
  18. 小程序之map地图上不能在覆盖层
  19. HBase原理和架构
  20. npm、webpack、vue-cli快速上手版

热门文章

  1. 日志之slf4j和logback日志系统(二)
  2. jquery获取窗口和文档的高度和宽度
  3. Xinetd服务的安装与配置详解
  4. 6:Partial Update 内部原理 和 乐观锁并发控制
  5. sqlite 一些常用的句子
  6. Mongodb 学习笔记(二) :索引
  7. M - Ordering Tasks(拓扑排序)
  8. 微信小程序之生成图片分享 二维码分享 canvas绘制
  9. Android笔记(五十九)Android总结:四大组件——Service篇
  10. 本地安装部署ActiveCollab