1071 Speech Patterns (25分)【map】

1071 Speech Patterns (25分)

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

题目大意:

令“单词”的定义为大小写字母、数字的组合。给出一个字符串,问出现次数最多的单词及其出现次数(一切除了大小写字母、数字之外的字符都作为单词的分隔符)。其中字母不区分大小写,且最后按小写字母输出。

例如样例中:1、can1出现1次。2、can出现5次(can1中的can不算)。3、a出现2次。4、it出现1次。

解题思路:

由题意可知,单词由大小写字母、数字组成(不妨称为有效字符),且由除有效字符外的字符进行分割,因此不妨枚举字符串中的字符,如果该字符是有效字符,则将其加入到当前单词中(如果是大写字母,则将其替换为对应的小写字母);如果该字符不是有效字符,则将当前单词的出现次数加1(使用map实现)。之后跳过非有效字符,进行下一个单词的组合。

#include<iostream>
#include<map>
#include<string>
#include<ctype.h>
using namespace std;

bool check(char c)
{
	if ((c >= '0'&&c <= '9') || (c >= 'A'&&c <= 'Z') || (c >= 'a'&&c <= 'z'))
		return true;
	else return false;
}

int main()
{
	map<string, int>count;
	string str;
	getline(cin, str);
	int i = 0;
	while (i < str.length())
	{
		string word;               //存储分解出来的单词
		while (i < str.length() && check(str[i]) == true)
		{
			if (str[i] >= 'A'&&str[i] <= 'Z')
			{
				str[i] = tolower(str[i]);              //将大写字母转化为小写字母 
			}
			word += str[i];
			i++;
		}
		if (word != "")     //单词非空
		{
			if (count.find(word) == count.end())
				count[word] = 1;
			else count[word]++;
		}
		while (i < str.length() && check(str[i]) == false)
		{
			i++;
		}
	}
	string ans;
	int MAX = 0;
	for (map<string, int>::iterator it = count.begin(); it != count.end(); it++)
	{
		if (it->second > MAX)
		{
			MAX = it->second;
			ans = it->first;
		}
	}
	cout << ans << " " << MAX << endl;
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

胡小涛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值