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 ...
随机推荐
- 观察者模式(二)--《Head First DesignPattern》
我们用Java中自带的观察者模式接口来重写前面的例子. 先看一下类图: 这里用到了一个setChanged函数,它用来标记状态已经改变的事实,好让notifyObservers()知道当它调用时就应该 ...
- Android(java)学习笔记113:Android编写代码调用Vibrator震动功能(Bug:按下按钮button始终没有震动)
1.之前我编写的代码是如下: package com.himi.vibrate; import android.app.Activity; import android.app.Service; im ...
- ArcGIS Server 10.2 实战(二)动态修改要素数据的地理处理服务
上一篇<ArcGIS Server 10.2 实战(一)Asp.net MVC与JSON数据妙用实现动态生成要素图层>介绍了如何用JSON转要素的地理处理服务,实现了动态创建点要素并加载到 ...
- mysql单引号和双引号的用法
表名,列名最好用`(esc下面那个,不用`会出错) 这就要从双引号和单引号的作用讲起:双引号里面的字段会经过编译器解释然后再当作HTML代码输出,但是单引号里面的不需要解释,直接输出.例如:$abc= ...
- 1045 | error connecting to master 'slave_user@192.168.0.75:3306' - retry-time: 6
mysql 主从复制问题整理 问题: 1045 | error connecting to master 'slave_user@192.168.0.75:3306' - retry-t ...
- 关于JDK中的总结和基本知识总结
人机交互的图形化界面(GUI) 命令行方式(CLI command line interface) JDK有不同的版本(linux,mac os, windows) Java 的跨平台性. 软件放到 ...
- SSH整合_struts.xml 模板
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "- ...
- poj 3177 边连通分量
思路: dfs求出所有点的low值,然后对每个连通分量进行缩点,可以通过low来进行缩点.虽然在同一连通分量里可能存在不同的low值,但这并不影响缩点.将每个连通分量缩为一个点后,只要求出这个缩点后的 ...
- 把CheckedListBoxControl设置为单选框
private void chkControl_ItemChecking(object sender, DevExpress.XtraEditors.Controls.ItemCheckingEven ...
- ORACLE多表查询优化
ORACLE有个高速缓冲的概念,这个高速缓冲就是存放执行过的SQL语句,那oracle在执行sql语句的时候要做很多工作,例如解析sql语句,估算索引利用率,绑定变量,读取数据块等等这些操作.假设高速 ...