pat 1071 Speech Patterns

原创 2015年11月20日 23:23:50

1071. Speech Patterns (25)

时间限制
300 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
HOU, Qiming

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


#include <iostream>
#include <map>
#include <vector>
#include <cstdio>
using namespace std;

int main() {
	map<string, int> name2num;

	string str="";
	getline(cin, str);

	int len = str.length(), i;
	string name="";
	for (i = 0; i < len; ++i)
	{	
		if(str[i] >= 'A' && str[i] <= 'Z') {
			name+= char(str[i]-'A'+'a');
		} else if(str[i] >= 'a' && str[i] <= 'z') {
			name += str[i];
		} else if(str[i] >= '0' && str[i]<='9') {
			name += str[i];
		}
		 else{
			if(name != "") {
				if(name2num.count(name)) {
					name2num[name] += 1;
				} else {
					name2num[name]=1;
				}
			}
			name="";
		}
		
	}

	if(name != "") {
		if(name2num.count(name)) {
			name2num[name] += 1;
		} else {
			name2num[name]=1;
		}
	}

	map<string, int>::iterator iter, tmp;
	int maxx = -1;

	for (iter = name2num.begin(); iter != name2num.end(); ++iter)
	{
		if(iter->second > maxx) {
			maxx = iter->second;
			tmp = iter;
		} else if(iter->second == maxx) {
			string name1 = iter->first;
			string name2 = tmp->first;

			if(name1 < name2) tmp = iter;
		}
	}

	cout << tmp->first << " " << tmp->second << endl;
	return 0;
}




More Effective C++ 条款28(中)

 条款28:灵巧(smart)指针(中) 测试灵巧指针是否为NULL目前为止我们讨论的函数能让我们建立、释放、拷贝、赋值、dereference灵巧指针。但是有一件我们做不到的事情是“发现灵巧指针为N...
  • zhc
  • zhc
  • 2001-12-12 09:42:00
  • 913

PAT 1071 Speech Patterns (字符串处理)

这个题只要知道一点就很容易了,即string类型的变量可以这样初始化string s(beg,end) 就可以了,当然这里的s是不包含end处的字符的,只包含beg到end-1处的字符串 1...
  • IAccepted
  • IAccepted
  • 2014-03-19 20:18:25
  • 1746

1071. Speech Patterns (25)【字符串操作】——PAT (Advanced Level) Practise

题目信息1071. Speech Patterns (25)时间限制300 ms 内存限制65536 kB 代码长度限制16000 BPeople often have a preference ...
  • xianyun2009
  • xianyun2009
  • 2015-12-27 23:05:52
  • 1012

PAT 1071. Speech Patterns

1. 输入时需要讲大写转换为小写,否则zuihou map, 记录
  • u014674776
  • u014674776
  • 2014-04-13 10:48:49
  • 843

1071. Speech Patterns 解析

getline把整行读入,大写变小写,然后去掉标点(标点变空格) 然后分割单词,用set和map建立映射,统计数目。 注意:单个单词的时候,需要单独判定。我是再转换的时候标记又没有空格,没有空格就是单...
  • sheepyWYY
  • sheepyWYY
  • 2017-03-01 16:33:24
  • 145

PAT 1071 Speech Patterns

卡时间卡的比较紧 #include #include #include #include #include #include #include #include #define ll long lo...
  • shllhsboa
  • shllhsboa
  • 2014-08-31 17:51:45
  • 318

【PAT甲级】1071. Speech Patterns (25)

#include #include #include #include using namespace std;int main() { string s, tmp; map ...
  • lambsnow
  • lambsnow
  • 2017-06-21 17:26:49
  • 84

pat 1071 Speech Patterns (25)

http://pat.zju.edu.cn/contests/pat-a-practise/1071 1071. Speech Patterns (25) 时间限制 300 ms ...
  • u014098060
  • u014098060
  • 2014-03-14 13:18:07
  • 543

pat advanced 1071 Speech Patterns

题目地址: http://pat.zju.edu.cn/contests/pat-a-practise/1071 思路比较简单,用map来记录,map默认是按字典序的。 但是前几次有两个case ...
  • flyupliu
  • flyupliu
  • 2014-01-29 18:07:44
  • 479

PAT 字符,数字处理题,总是有坑,边界,特殊,时间复杂度

1054. 求平均值 (20)题目地址https://www.patest.cn/contests/pat-b-practise/1054注意点 输入输出 case4 把 “1.” 当成正确的 (坑)...
  • qq_26437925
  • qq_26437925
  • 2016-10-27 11:05:58
  • 573
收藏助手
不良信息举报
您举报文章:pat 1071 Speech Patterns
举报原因:
原因补充:

(最多只允许输入30个字)