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 ...
随机推荐
- java命令模式
命令模式 Command Pattern(Another Name:Action,Transaction) Encapsulate a request as an object ,thereby le ...
- android开发之路07(无硝烟的战争)
如何做一名优秀的android面试官? 如何做一名优秀的android候选者? 提到这个问题我不得不提起我们小升初,初升高,高生升本这几个历程中我们与出题人之间的无硝烟的战争.我们总是为自己的成绩担心 ...
- MAC mysql安装及设置
MAC下安装MYSQL有两种方式,一种为压缩包形式 另一种为.dmg文件安装包 . 首先先介绍压缩包形式的安装方法: 去MySql官网下MySQL classic版mysql-5.1.54-osx ...
- ibatis中isEquals、isNotEmpty的用法
一:isEquals用于动态拼接sql 如下实例: 如果status的状态为0,则更新attribute1;状态为1,则更新attribute2; 状态为2,则更新attribute3. <up ...
- Eclipse遇到Initializing Java Tooling解决办法
可参考博客:http://liaojuncai.iteye.com/blog/1954113 打开Eclipse的时候莫名其妙的出现这个错误,进度条一直显示这个提示Initializing java ...
- 转:艾瑞咨询2016 IM云的发展趋势
转自: http://www.cnblogs.com/lingyunhu/p/rtc63.html
- 解决c#,wpf程序带环境安装包体积太大问题
在.net开发客户端的时候,一定会遇到用户没有安装.net环境的问题,特别是现在win7,win8,win10多系统并用的时间段,很多开发者使用的是4.5的目标环境,用户却是使用win7的系统,这样势 ...
- HTML+CSS3 纯代码实现转盘效果
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- asp.net mvc开发的社区产品相关开发文档分享
分享一款基于asp.net mvc框架开发的社区产品--近乎.目前可以在官网免费下载,下载地址:http://www.jinhusns.com/Products/Download?type=whp 1 ...
- Ajax-数据格式-html