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)的更多相关文章

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

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

  2. 1071 Speech Patterns (25)(25 分)

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

  3. PAT Advanced 1071 Speech Patterns (25 分)

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

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

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

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

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

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

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

  7. PAT 1071 Speech Patterns[一般]

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

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

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

  9. 1071 Speech Patterns

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

随机推荐

  1. DTMF的原理分析

    转自:http://blog.csdn.net/wangwenwen/article/details/8264925 1. DTMF原理 DTMF(Double Tone MulitiFrequenc ...

  2. Spring容器初始化过程

    一.Spring 容器高层视图 Spring 启动时读取应用程序提供的Bean配置信息,并在Spring容器中生成一份相应的Bean配置注册表,然后根据这张注册表实例化Bean,装配号Bean之间的依 ...

  3. linux 用户之间的切换

    从root用户切换到普通用户fxm, 使用如下命令:su - fxm 从普通用户切换到root用户,使用如下命令:su - 或者 su, root可以省略不写.

  4. View(三)

    大家在平时使用View的时候都会发现它是有状态的,比如说有一个按钮,普通状态下是一种效果,但是当手指按下的时候就会变成另外一种效果,这样才会给人产生一种点击了按钮的感觉.当然了,这种效果相信几乎所有的 ...

  5. Linux3.18.6内核添加系统调用(32位系统)

    1.将已经下载好的内核解压,我的目录是 /home/shangsongchao/LinuxKernel/testlinux-3.18.6/linux-3.18.6 2.添加系统调用表: 在/home/ ...

  6. Oracle注入漏洞

    sqlmap.py -u "http://10.7.82.123:9104/servlet/json" --cookie="JSESSIONID=abcgk26KDf_5 ...

  7. 从客户端(CourseIssueContent="<P>财务审计师岗位认证招生简章<BR>...")中检测到有潜在危险的 Request.Form 值。

    说明: 请求验证过程检测到有潜在危险的客户端输入值,对请求的处理已经中止.该值可能指示危及应用程序安全的尝试,如跨站点的脚本攻击.通过在 Page 指令或 配置节中设置 validateRequest ...

  8. hdu 5269 ZYB loves Xor I

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission( ...

  9. Java在ACM中的应用

    Java在ACM中的应用 —. 在java中的基本头文件(java中叫包) import java.io.*; import java.util.*; //输入Scanner import java. ...

  10. Java第一天:安装搭建Java开发环境

    Java是面向对象的语言.它是通过虚拟机的运行机制来实现“跨平台”的. 这里不多说其他的,进入正题先,学习任何语言前的第一步都是要先搭建好开发环境,Java开发环境搭建如下: 1.到官网 http:/ ...