1071 Speech Patterns (25)(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
#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 分)的更多相关文章
- PAT 1071 Speech Patterns[一般]
1071 Speech Patterns (25 分) People often have a preference among synonyms of the same word. For exam ...
- PAT 甲级 1071 Speech Patterns (25 分)(map)
1071 Speech Patterns (25 分) People often have a preference among synonyms of the same word. For ex ...
- 1071 Speech Patterns——PAT甲级真题
1071 Speech Patterns People often have a preference among synonyms of the same word. For example, so ...
- PAT Advanced 1071 Speech Patterns (25 分)
People often have a preference among synonyms of the same word. For example, some may prefer "t ...
- 【PAT甲级】1071 Speech Patterns (25 分)(getline(cin,x))
题意: 输入一行字符串,输出出现过次数最多的由字母和数字组成的字符串以及它出现的次数(对大小写不敏感,输出全部输出小写). AAAAAccepted code: #define HAVE_STRUCT ...
- 1071. Speech Patterns (25)
People often have a preference among synonyms of the same word. For example, some may prefer "t ...
- PAT (Advanced Level) 1071. Speech Patterns (25)
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
- PAT甲题题解-1071. Speech Patterns (25)-找出现最多的单词
分割字符串的用法+map映射给出input中出现次数最多的单词,如果次数相同,给出按字典序最小的. 这里我用了自定义分隔符来读取字符串,方法如下: //按照定义的分隔符d来分割字符串,对str进行读取 ...
- 1071 Speech Patterns
People often have a preference among synonyms of the same word. For example, some may prefer "t ...
随机推荐
- 获取SpringMVC中所有RequestMapping映射URL信息
SpringMVC启动的时候,会把接口信息收集在RequestMappingHandlerMapping中,故可以通过这个类,拿到全部的映射信息,Sample代码段如下: @Autowired pri ...
- How to read request body in a asp.net core webapi controller?
原文 How to read request body in a asp.net core webapi controller? A clearer solution, works in ASP.Ne ...
- 2.8_Database Interface ADO由来
OLE-DB,它无法广为流行,因为如下两点: 1.由于OLE-DB太底层化,使用上非常复杂,需要程序员拥有高潮的技巧. 2.OLEDB标准的API是C++API,只能供C++语言调用. 为了使得流行的 ...
- NIO开发Http服务器(3):核心配置和Request封装
最近学习了Java NIO技术,觉得不能再去写一些Hello World的学习demo了,而且也不想再像学习IO时那样编写一个控制台(或者带界面)聊天室.我们是做WEB开发的,整天围着tomcat.n ...
- Math对象的一些方法
ceil(n) 返回n向上取整的最近的整数floor(n) 返回n向下取整到最近的整数max(a,b,c...) 返回最大值min(a,b,c...) 返回最小值round(n) 返回n四舍五入的最近 ...
- JavaScript之控制标签内容
function abb(a){ return document.getElementById(a); } console.log(abb('box').innerHTML); 标签.innerHTM ...
- 社交类app开发( 仿陌陌 客户端+服务器端)
一.开发所需要的技术 手机端需要Android/iOS开发人员,服务器端需要php/数据库开发人员, 如果再做网页版的话,WEB开发也是要的. 即时通讯 GPS地图 群聊 差不多 对 http so ...
- robot framework笔记(三):扩展SeleniumLibrary库 (自定义关键字)
(一)自定义和浏览器相关的关键字 以下代码GitHub 版本库地址: https://github.com/blairwind/blog_rf SeleniumLibrary的扩展文档中提供了3种增加 ...
- 交叉编译openssl1.1.1a
交叉编译openssl1.1.1a的时候遇到的问题,记录一下,方便下次查找 一.下载源码 1.打开openssl官网,下载openssl-1.1.1.tar.gz源码包. 2.执行下面的命令解压源 ...
- Flask之flask-sqlalchemy
接下来基于这个Flask项目,我们要加入Flask-SQLAlchemy让项目变得生动起来 1.加入Flask-SQLAlchemy第三方组件 from flask import Flask # 导入 ...