1071 Speech Patterns (25分) 第三个测试点 + 别人的代码

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

题意:统计一句话的单词个数,这句话的单词被非字母、数字字符分割,不管是开头、中间、结尾,都有可能。

这也是第三个测试点wa的原因

如: aa***aaaa  应该分割成两个单词。

我的代码:

#include<iostream>
#include<map>
#include<cstring>
#include<algorithm>
#include<vector>
#include<sstream>
using namespace std;
struct node{
	int cnt;
	string str;
};
int cmp(node x,node y)
{
	if(x.cnt==y.cnt)
	return x.str<y.str;
	else
	return x.cnt>y.cnt;
}
int main()
{
	string s;
	int i,j,k;
	map<string,int> a;
	vector<node> b;
	node t;
	a.clear();
	b.clear();

	
	getline(cin,s);
	stringstream ss(s);
	while(ss>>s)
	{
	    string tt;
	    for(i=0;i<s.length();i++)
	    {
	    	if(isalpha(s[i])||isdigit(s[i]))
	    	{
	    		if(s[i]>='A'&&s[i]<='A'+25)
	    		{
	    			s[i]+=32;
				}
				tt+=s[i];
			}
			else
			if(tt.empty()==0)
			{
				a[tt]++;
//				cout<<tt<<endl;
				tt.clear();
			}
		}
		if(tt.empty()==0)   // 这很有必要加一个判断 
		{
			a[tt]++;
			tt.clear();
		}
	}
	for(map<string,int>::iterator it=a.begin();it!=a.end();it++)
	{
//		cout<<it->first<<"  "<<it->second<<endl;
		t.str=it->first;
		t.cnt=it->second;
		b.push_back(t);
	}
	sort(b.begin(),b.end(),cmp);
	cout<<b[0].str<<" "<<b[0].cnt<<endl;
	return 0;
 } 

我解释下这个地方:

这个地方是把形如:???aaaa  这样的单词放到map里去。

因为这样单词结尾也是字母或数字,当轮到最后一个合法字符时,它直接进了

而不会执行下面的else。

所以有必要在外面再加一个字符串非空判断。

别人的代码:

#include <iostream> 
#include <map>
#include <string> 
using namespace std;
bool check(char c){
	if(c>='0'&&c<='9')	return true;
	if(c>='A'&&c<='Z')	return true;
	if(c>='a'&&c<='z')	return true;
	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]+=32;
			}
			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;
}
发布了374 篇原创文章 · 获赞 101 · 访问量 21万+

猜你喜欢

转载自blog.csdn.net/qq_41325698/article/details/103424661