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

题目和这个简直一摸一样
在这里插入图片描述
①c语言,暴搜:
但是暴搜有个节点过不了

#include<stdio.h>//需要改进输入 
#include<string.h>
#include<ctype.h>
char ch[1050000],temp[200],word[800][100],max[800][100];//temp是临时的单词存储,ch是读入每行的句子. 
int cnt[2000]={0};
int main()
{        //  len是每行字符串的长度. 
	
   	int sh,maxn=0;
	int i,j=0,k=0,num=0,len; //num是已存单词的数量.比如  sb like like sports;  那么num=3; 
	while((sh=getchar())!=EOF)
		ch[i++]=sh;
	len=strlen(ch);
		for(i=0;i<len;i++)
		{     
			if( isalnum(ch[i]))    //如果读到字母. 
			{
				temp[k]=tolower(ch[i]);
				k++;
			}
			
			if (!isalnum(ch[i])&&isalnum(ch[i-1]))  //如果读到一个非字母且前一个字符是字母,那么  存入新单词或者 统计出现次数. 
			{
				temp[k]='\0';  //存入 结束符 把字符数组变成字符串. 
				for(j=0;j<num;j++) //num是已存单词的数量 
				{
					if(  strcmp(temp,word[j])==0  )  //如果strcmp的 返回值为0,这是一个已存入的单词,统计量+1 
					{ 
						cnt[j]++;   //找到了则提前结束遍历
					  	k=0;
					  	break;
					}	
				}   
				//如果遍历结束没有找到对应的单词,存入新单词 
				if(j==num)
				{
					//这是一个新单词,存入,num++; 
					strcpy(word[j],temp);
					k=0;
					cnt[j]++;  
					num++;
				}
		    }
		}
    int	index=0;
    for(i=1;i<num;i++)
    	if(cnt[i]>cnt[index]) 
			index=i;
    k=0;
	for(i=0;i<num;i++)
		if(cnt[i]==cnt[index])
			strcpy(max[k++],word[i]);
	int  sub=0;  //sub是字典序最小的下标. 
	for(i=1;i<k;i++) 
	    if(strcmp(max[sub],max[i])>0)
			sub=i;	
	printf("%s %d",max[sub],cnt[index]);
}

在这里插入图片描述
②c++:stl map映射(用unordered_map也行)

#include<iostream>
#include<map>
#include<cctype>
using namespace std;
int main()
{
	string s,temp,max_word;
	getline(cin,s);
	s+=" \n";//预置结束标志 
	map<string,int>mapp;
	int max_time=-1,i=0;
	while(s[i]!='\n')//读到'\n'就结束 
	{
		s[i]=tolower(s[i]);
		if(isalnum(s[i])) temp+=s[i];
		else if(!temp.empty())
		{
			mapp[temp]++;
			if(mapp[temp]>max_time)//如果出现次数比最大值大,更新最大值 
				max_time=mapp[temp],max_word=temp;
			else if(mapp[temp]==max_time)//如果出现次数等于最大次数,根据字典序选择是否更新	
				if(max_word>temp)max_word=temp;
			temp.clear();//清空temp,进行下一次存储; 
		}
		i++;//指针移向下一个字符
	}
		printf("%s %d",max_word.c_str(),max_time); 
}
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页