这题非常基础,边创建字典树边找出出现次数最多的单词,然后记录下来,下面贴代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
int count;
struct node *next[26];
};
int max=0;
char ch[20];
node *init()
{
node *p;
p=(node*)malloc(sizeof(node));
p->count=0;
for(int i=0;i<26;i++)
p->next[i]=NULL;
return p;
}
void insert(node *root,char s[20])
{
int i,t,len;
node *p=root;
len=strlen(s);
for(i=0;i<len;i++)
{
t=s[i]-'a';
if(p->next[t]==NULL)
p->next[t]=init();
p=p->next[t];
}
p->count++;
if(p->count>max)
{
max=p->count;
strcpy(ch,s);
}
}
int main()
{
int i,N;
char s[20];
scanf("%d",&N);
node *root=init();
for(i=0;i<N;i++)
{
scanf("%s",s);
insert(root,s);
}
printf("%s %d\n",ch,max);
}