Speech Patterns (string)
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
例子是个坑,一开始认为 格式是can1:”_______” ______为要检查的内容。
其实“ne character from the set [0-9 A-Z a-z]”,所以“can1”是一种字符,有别于“can”
还有就是容易超时,后来想了一个O(n)的算法,如下:
#include <iostream>
#include <map>
#include <string>
using namespace std;
struct word
{
int time;
int len;
};
map<string,word> mm;
void fun1(string ss)
{
int i=;string tem="";
while(i<ss.length())
{
while(i<ss.length())
{
if((ss[i]>='A'&&ss[i]<='Z')||(ss[i]>='a'&&ss[i]<='z')||(ss[i]>=''&&ss[i]<=''))
{
if(ss[i]>='A'&&ss[i]<='Z')
ss[i]=ss[i]-'A'+'a';
tem+=ss[i];
++i;
}
else break;
}
++mm[tem].time;
mm[tem].len=i;
tem="";
while(i<ss.length())
{
if((ss[i]>='A'&&ss[i]<='Z')||(ss[i]>='a'&&ss[i]<='z')||(ss[i]>=''&&ss[i]<=''))
{
break;
}
else i++;
}
}
}
void fun2(string ss)
{
int i=;string tem="";
while(i<ss.length())
{
while(i<ss.length())
{
if((ss[i]>='A'&&ss[i]<='Z')||(ss[i]>='a'&&ss[i]<='z')||(ss[i]>=''&&ss[i]<=''))
break;
else i++;
}
while(i<ss.length())
{
if((ss[i]>='A'&&ss[i]<='Z')||(ss[i]>='a'&&ss[i]<='z')||(ss[i]>=''&&ss[i]<=''))
{
if(ss[i]>='A'&&ss[i]<='Z')
ss[i]=ss[i]-'A'+'a';
tem+=ss[i];
++i;
}
else break;
}
++mm[tem].time;
mm[tem].len=i;
tem="";
}
}
int main()
{
string ss;
int i,j;int c1,c2;
while(getline(cin,ss))
{
mm.clear();
if((ss[]>='A'&&ss[]<='Z')||(ss[]>='a'&&ss[]<='z')||(ss[]>=''&&ss[]<=''))
fun1(ss);
else fun2(ss);
int Max=;int Min=;
string most;
map<string,word>::iterator it;
for(it=mm.begin();it!=mm.end();it++)
{ if((it->second).time>Max||((it->second).time==Max&&(it->second).len<Min))
{
Max=(it->second).time;
Min=(it->second).len;
most=it->first;
}
}
cout<<most<<" "<<Max<<endl;
}
return ;
}
Speech Patterns (string)的更多相关文章
- Pat1071: Speech Patterns
1071. Speech Patterns (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HOU, Qiming Peo ...
- 【算法笔记】A1071 Speech Patterns
1071 Speech Patterns (25 分) People often have a preference among synonyms of the same word. For ex ...
- 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 ...
- PAT_A1071#Speech Patterns
Source: PAT A1071 Speech Patterns (25 分) Description: People often have a preference among synonyms ...
- 1071 Speech Patterns——PAT甲级真题
1071 Speech Patterns People often have a preference among synonyms of the same word. For example, so ...
- 1071. Speech Patterns (25)
People often have a preference among synonyms of the same word. For example, some may prefer "t ...
- A1071. Speech Patterns
People often have a preference among synonyms of the same word. For example, some may prefer "t ...
- 1071 Speech Patterns
People often have a preference among synonyms of the same word. For example, some may prefer "t ...
随机推荐
- Flask的部署
当前对部署flask的一些学习: 1.全局安装nginx 1.1 nginx的配置文件怎么写? $ sudo rm /etc/nginx/sites-enabled/default $ sudo to ...
- 探讨PHP页面跳转几种实现技巧
PHP被许多程序员用来开发WEB的首选语言.在实际开发中,网站的各项功能都可以通过PHP语言的编写来满足,比如PHP页面跳转这一方法. 探讨PHP变量解析顺序如何获取提交数据 深入解读PHP运行机制 ...
- linux-redhat5找回root密码
我在虚拟机里装了个redhat(RedhatEnterpriseLinuxASv5.4-x64),也不经常用,偶尔进去用用吧,又把密码忘记了,哎...脑子不好使啊 不知道像ubuntu是不是一样的,还 ...
- Web前端学习笔记2
一.开发工具sublime的常用快捷键. 1. 快捷键 功能 ctrl+shift+D 快速复制 ctrl+L 快 ...
- SSRS:之为用户“NT AUTHORITY\NETWORK SERVICE”授予的权限不足,无法执行此操作。 (rsAccessDenied)
错误信息: 为用户"NT AUTHORITY\NETWORK SERVICE"授予的权限不足,无法执行此操作. (rsAccessDenied) 如图: 解决方案之检查顺序: 1. ...
- ASP多行多列显示代码
<table width="98%" border="0" align="center"> <tr> <% S ...
- 解决MS Office下载网站数据失败的问题
最近遇到在MS Excel中建立的Web Query在创建完成后过了一段时间(或关闭文件后再次打开文件并刷新数据)出现无法刷新的问题,点击刷新时报错如下: 无法下载您要求的信息. 这是一个很不友好的报 ...
- AndroidStudio KeyMap
- python学习day2--python基础
python没有常量的定义方式,一般常量可人为用全部大写字母来表示. 如: MYSQL_CONNECTION='192.168.10.2' 以os库为例: import os os.system('i ...
- .NET微信支付(H5仅限公众号支付)
闲来无事,恰好有一个要用微信公众平台支付的功能,研究来研究去,就是要细心和多看腾讯提供的文档.当然有几个坑是很有必要说明一下的 公众号支付,这里统一叫H5支付,以下都是. 在做H5支付的时候,第一步就 ...