关闭

pat 1071 Speech Patterns

178人阅读 评论(0) 收藏 举报
分类:

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




0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:111015次
    • 积分:4675
    • 等级:
    • 排名:第6221名
    • 原创:378篇
    • 转载:4篇
    • 译文:0篇
    • 评论:3条
    文章分类
    最新评论