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 ...
随机推荐
- Firefox便携版
安装firefox后默认配置文件在C:\Users\Administrator\AppData\Roaming\Mozilla\Firefox 可以在快捷方式里设置不同的配置文件,但是要注意参数的含义 ...
- iOS xcode6添加预编译文件
在xcode6以后,由于苹果不建议开发者乱用预编译文件,所以,在项目创建之后 就不会自动生成预编译文件. 那么如果我们想要使用预编译文件,就需要自己动手来添加.那到底该如何为我们的项目添加预编译文件呢 ...
- 关于js中event的target和currentTarget的区别
今天又遇到这个问题了,总是搞不清楚target和currentTarget的区别,百度搜索的时候看到一遍文章解释得很清楚,特意记录下录,以备不时之需: target与currentTarget的区别? ...
- 关于JDK中的集合总结(一)
静态方法只能继承,不能重写(Override). StringBufffer,数组,对象 都是容器. 加入数据,“zhangsan” true ,67, 三个数据数据类型不同,不能用数组作为集合,只能 ...
- IOS下视频监控项目总结
一.项目说明 二.技术点 1.数据缓存 2.storyboard的使用 3.h264码流数据的解码 4.UDP通讯 三.解决方案 四.总结 五.相关资料
- Nginx HTTP User_agent
假设我这里有大量图像.CSS.javascript等静态文件,分别放在后端服务器 192.168.1.5 和 192.168.1.6上,那么我如何利用nginx的反向代理功能将不同的 http_us ...
- 【转】【CDC翻客】移动端App测试实用指南
译者注:本文从测试人员的角度出发,提出了100多个在测试移动App过程中需要考虑的问题.不管你是测试人员.开发.产品经理或是交互设计师,在进行移动App开发时,这些问题都很有参考价值.我和Queen ...
- 转 Android 4.0后,自定义Title报错 You cannot combine custom titles with other title feature
自定义Titlebar时为了避免冲突 需要修改:AndroidManifest.xml android:theme="@style/mystyle" styles.xml文件中 ...
- Unity User Group 北京站图文报道:《Unity3D VR游戏与应用开发》
很高兴,能有机会回报Unity技术社区:我和雨松MOMO担任UUG北京站的负责人, 组织Unity技术交流和分享活动. 本次北京UUG活动场地–微软大厦 成功的UUG离不开默默无闻的付出:提前2小时到 ...
- js 获取 当前时间 时间差 时间戳 倒计时
开发web一段时间后发现经常使用时间进行一些操作,比较多的就是获取当前时间.对时间进行比较.时间倒计时.时间戳这些部分,每次去用经常忘总是需要去查询,还是自己总结一下比较靠谱. 获取时间戳的方法: 第 ...