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;
}




pat advanced 1071 Speech Patterns

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

PAT (Advanced Level) 1071. Speech Patterns (25) 字符串处理

People often have a preference among synonyms of the same word. For example, some may prefer "the po...

Pat(Advanced Level)Practice--1071(Speech Patterns)

Pat1071代码 题目描述: People often have a preference among synonyms of the same word. For example,...

PAT 1071 Speech Patterns (字符串处理)

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

PAT 1071. Speech Patterns

1. 输入时需要讲大写转换为小写,否则zuihou map, 记录

PAT A 1071. Speech Patterns (25)

题目

PAT A1071. Speech Patterns (25)

People often have a preference among synonyms of the same word. For example, some may prefer "the po...
  • jolivan
  • jolivan
  • 2017年02月21日 18:44
  • 78

【C++】PAT(advanced level)1071. Speech Patterns (25)

1071. Speech Patterns (25) 时间限制 300 ms 内存限制 32000 kB 代码长度限制 16000 B ...

【PAT】1071. Speech Patterns (25)

People often have a preference among synonyms of the same word. For example, some may prefer "the po...

PAT 1071. Speech Patterns (25)

1071. Speech Patterns (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:pat 1071 Speech Patterns
举报原因:
原因补充:

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