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 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
思路:使用map保存字符串出现的个数
注意:不要使用scanf和cin来读取字符串,只能通过一个个的读取字符。比如:输入了asd*^&123,如果用scanf("%s",s)会有问题
#include <iostream>
using namespace std;
#include<string>
#include<map>
#include<algorithm>
#include<vector>
typedef struct node{
int num;
string s;
}Node;
bool compareNode(Node n1, Node n2){
if(n1.num>n2.num||(n1.num==n2.num)&&n1.s.compare(n2.s)<0){
return true;
}
return false;
}
int main()
{
char c;
string s="";
map<string,int> m;
c=getchar();
while(c!='\n'){
if(c>='0'&&c<='9'||c>='a'&&c<='z')
s+=c;
else if(c>='A'&&c<='Z'){
s+=(c-'A'+'a');
}
else{
if(s.compare("")!=0)
m[s]++;
s="";
}
c= getchar();
}
m[s]++;//缺少该句,会报段错误,考虑这样的输入:*******S
vector<Node> r;
map<string, int> ::iterator iter;
for(iter=m.begin();iter!=m.end();iter++){
Node n;
n.s = iter->first;
n.num = iter->second;
r.push_back(n);
}
sort(r.begin(),r.end(),compareNode);
cout<<r[0].s<<" "<<r[0].num<<endl;
return 0;
}
1071. Speech Patterns (25)的更多相关文章
- 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 (25)(25 分)
People often have a preference among synonyms of the same word. For example, some may prefer "t ...
- PAT Advanced 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进行读取 ...
- 【PAT甲级】1071 Speech Patterns (25 分)(getline(cin,x))
题意: 输入一行字符串,输出出现过次数最多的由字母和数字组成的字符串以及它出现的次数(对大小写不敏感,输出全部输出小写). AAAAAccepted code: #define HAVE_STRUCT ...
- PAT 1071 Speech Patterns[一般]
1071 Speech Patterns (25 分) People often have a preference among synonyms of the same word. For exam ...
- 1071 Speech Patterns——PAT甲级真题
1071 Speech Patterns People often have a preference among synonyms of the same word. For example, so ...
- 1071 Speech Patterns
People often have a preference among synonyms of the same word. For example, some may prefer "t ...
随机推荐
- 应用程序调试工具gdb,王明学learn
应用程序调试工具gdb学习使用 一.GDB简介 GDB 是 GNU 发布的一款功能强大的程序调试工具.GDB 主要完成下面三个方面的功能: 1.启动被调试程序. 2.让被调试的程序在指定的位置停住. ...
- POJ——3264线段树
题目: 输入两个数(m,n),m表示牛的头数,n表示查询的个数.查询时输入两个数(x,y),表示查询范围的起始值和终止值,查询结果是,这个区间内牛重量的最大值减去牛重量的最小值,数量级为1000,00 ...
- 图解LoadAverage(负载)
图解LoadAverage(负载) http://www.habadog.com/2015/02/27/what-is-load-average/ 一.什么是Load Average 系统负载(S ...
- loadrunner实现浮点型数据转换成字符串
ftoa(float floatNum, char *convFloatString) { char new[10]; float number,dTemp,temp_val; int base, f ...
- AngularJS学习之模型
1.ng-model指令:可以将输入域的值与AngularJS创建的变量绑定,用于绑定应用程序数据到HTML控制器(input,select,textarea)的值: <div ng-app=& ...
- coffeeScript学习01
安装 这里使用node.js npm install -g coffee-script # watch and compile coffee -w --output lib --compile src ...
- blade用法
一.blade条件判断,foreach循环写法 @if(isset($fileInfo) && !empty($fileInfo)) @foreach($fileInfo as $k) ...
- 移动终端app测试点总结
以下所有测试最后必须在真机上完整的执行1.安装.卸载测试 在真机上的以及通过91等第三方的安装与卸载 安装在手机上还是sd卡上 2.启动app测试3.升级测试 数字签名.升级覆盖安装.下载后手动覆盖安 ...
- 1、Delphi 打开目录和txt文件模块
//1.打开目录和打开txt文件 procedure TMainForm.bbtnOpenLoClick(Sender: TObject); var sLogName: string; begin s ...
- UVa1515 Pool construction(最小割)
题目 Source https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...