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】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 ...

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

题目信息1071. Speech Patterns (25)时间限制300 ms 内存限制65536 kB 代码长度限制16000 BPeople often have a preference ...

PAT (Advanced Level) Practise 1071 Speech Patterns (25)

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

浙江大学PAT_甲级_1071. Speech Patterns (25)

题目链接:点击打开链接 People often have a preference among synonyms of the same word. For example, some m...

1071. Speech Patterns (25)-PAT甲级真题(map应用)

1071. Speech Patterns (25) People often have a preference among synonyms of the same word. For exam...

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

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

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,...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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