1071. Speech Patterns (25)

1.题目求一段string中,出现次数最高的word,word可包括数字和字母。Here a “word” is defined as a continuous sequence of alphanumerical characters separated by non-alphanumerical characters or the line beginning/end.

2.首先是字符串处理,把大写字母的全改为小写字母。

3.字符串分隔,遇到不是数字或者字母的字符,就进行分隔。

4.使用map来存储次数,最后再存到vector中排序输出。

5.需要使用scanf读取字符,不能使用getline。

1071

时间限制
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<string>
//#include<stack>
//#include<unordered_set>
//#include <sstream>
//#include "func.h"
//#include <list>
#include <iomanip>
#include<unordered_map>
#include<set>
#include<queue>
#include<map>
#include<vector>
#include <algorithm>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
#include<limits.h>
#include<stack>
using namespace std;
/*
Here a "word is defined as a continuous sequence of alphanumerical characters separated by non-alphanumerical characters or the line beginning/end

*/
bool cmp(const pair<string, int>&a,const pair<string, int>&b)
{
	if (a.second > b.second) return true;
	else if (a.second == b.second && a.first < b.first) return true;
	else return false;
}
int main(void)
{
	string s="";
	char c = '1';
	while (c!='\n')
	{
		scanf("%c", &c);
		s += c;
	}
	
	map<string, int> times;
	string word = "";
	for (int i = 0; i < s.size(); i++)
	{
		if ((s[i] >= 'a'&&s[i] <= 'z') || (s[i] >= 'A'&&s[i] <= 'Z') || (s[i] >= '0'&&s[i] <= '9'))
		{//是字母
			if ((s[i] >= 'A'&&s[i] <= 'Z'))
				s[i] = s[i] - 'A' + 'a';
			word += s[i];
		}
		else
		{//不是字母,则清空word
			if (word != "")
				times[word]++;
			word = "";
		}
	}

	vector<pair<string, int>> ans(0);
	for (map<string, int>::iterator ite = times.begin(); ite != times.end(); ite++)
	{
		ans.push_back( *ite);
	}
	sort(ans.begin(), ans.end(), cmp);

	cout << ans[0].first << " " << ans[0].second << endl;

	return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注