A
Time Limit: 60ms Memory limit: 65536K 有疑问?点这里^_^
题目描述
给出n(1<= n && n <= 2*10^6)个字符串,每个字符串只包含小写英文字母,且最多有五个。问这n个字符串中出现次数最多的有多少个。
输入
单组输入。第一行输入一个数字n,接下来n行,每行包含一个字符串。
输出
输出一个数字代表答案。
示例输入
5
aba
abb
w
aba
z
示例输出
2
60ms过10^6的数据,这个必然是一个数据结构题,本题用字典树,以前学的时候还会写,但是后来一直没用到它,就渐渐的淡忘了,没想到今天见了,居然栽到这上面了...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
int flag;
struct node *next[26];
}*head;
char s[6];
int mx=-1;
void creat()
{
int l,i,j,ls;
struct node *p,*q;
p=(struct node *)malloc(sizeof(struct node));
p=head;
l=strlen(s);
for(i=0;i<l;i++)
{
ls=s[i]-'a';
if(p->next[ls]==NULL)
{
q=(struct node *)malloc(sizeof(struct node));
for(j=0;j<26;j++)
{
q->next[j]=NULL;
}
q->flag=0;
p->next[ls]=q;
}
p=p->next[ls];
}
p->flag++;
if(mx<p->flag)
mx=p->flag;
}
int main()
{
int i,n;
head=(struct node *)malloc(sizeof(struct node));
for(i=0;i<26;i++)
{
head->next[i]=NULL;
}
head->flag=0;
scanf("%d%*c",&n);
for(i=0;i<n;i++)
{
scanf("%s",s);
creat();
}
printf("%d\n",mx);
return 0;
}
不想多解释什么,会链表的孩子应该会这个...

本文讨论了如何使用字典树解决字符串频率统计问题,包括输入与输出规范、代码实现细节及优化策略。
537

被折叠的 条评论
为什么被折叠?



