1071. Speech Patterns (25)

时间:2022-04-02 23:56:01

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&&num;160&semi;Speech Patterns&&num;160&semi;&lpar;25&&num;160&semi;分&rpar;(map)

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

  2. 1071 Speech Patterns &lpar;25&rpar;(25 分)

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

  3. PAT Advanced 1071 Speech Patterns &lpar;25 分&rpar;

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

  4. PAT &lpar;Advanced Level&rpar; 1071&period; Speech Patterns &lpar;25&rpar;

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

  5. PAT甲题题解-1071&period; Speech Patterns &lpar;25&rpar;-找出现最多的单词

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

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

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

  7. PAT 1071 Speech Patterns&lbrack;一般&rsqb;

    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. HBase笔记:对HBase原理的简单理解

    早些时候学习hadoop的技术,我一直对里面两项技术倍感困惑,一个是zookeeper,一个就是Hbase了.现在有机会专职做大数据相关的项目,终于看到了HBase实战的项目,也因此有机会搞懂Hbas ...

  2. PHP 中的Closure

    PHP 中的Closure Closure,匿名函数,又称为Anonymous functions,是php5.3的时候引入的.匿名函数就是没有定义名字的函数.这点牢牢记住就能理解匿名函数的定义了. ...

  3. 【maven 报错】maven项目执行maven install时报错Error assembling WAR&colon; webxml attribute is required &lpar;or pre-existing WEB-INF&sol;web&period;xml if executing in update mode&rpar;

    在使用maven新建的web项目中,执行 执行如上的这两个操作,报错: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-co ...

  4. poj3616 LIS变形

    题目链接:http://poj.org/problem?id=3616 题意:给出m组数据a,b,c代表在第a分钟到第b分钟产生c个效益,问最大产生多少效益(区间不能重叠,每次工作完必须歇息R分钟) ...

  5. Mvc4页面缓存设置Cookie导致缓存失效

    [OutputCache(Duration = 60, VaryByParam = "none")]        public ActionResult Index()      ...

  6. keil优化论

    谈到优化,其实很多人都哭笑不得,因为在一个C51软件工程师的生涯中,总要被KEIL的优化耍那么一次到几次.我被耍过,想必看着文章的你也被耍过,如果你回答说不,那只能说你写的C51程序不多! 看看KEI ...

  7. C&num; 设置程序启动项

    托盘图标设置 新建一个NotifyIcon,会在托盘处显示一个图标. NotifyIcon.Icon可以直接设置一个ico图片,也可以延用原有程序的图标. notifyIcon.Icon = Syst ...

  8. 理解block和inode

    什么是block和inode? 定义:block就像是杯子 inode就像是杯子的编号,因为杯子太多了 1.根据文件的大小,在磁盘中储存时会占用一个或多个block:那么究竟多大的文件会使用一个blo ...

  9. appium 使用环境安装配置记录

    一.安装配置Java (cmd输入java,回车,没有出现“不是内部或外部命令,也不是可运行的程序或批处理文件”,即为成功) 二.安装node.js (cmd输入node -v,显示版本号即为成功) ...

  10. PHP7 网络编程(四)signal信号【待】

    https://blog.csdn.net/summy_j/article/details/73199069