【2892】A SDUTOJ
A
Time Limit: 60ms Memory limit: 65536K 有疑问?点这里^_^
题目描述
给出n(1<= n && n <= 2*10^6)个字符串,每个字符串只包含小写英文字母,且最多有五个。问这n个字符串中出现次数最多的有多少个。
输入
单组输入。第一行输入一个数字n,接下来n行,每行包含一个字符串。
输出
输出一个数字代表答案。
示例输入
5 aba abb w aba z
示例输出
2
字典树问题(模板型题目)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node
{
int flag;
struct node *next[26];
}*head;
char str[1000100];
int ma=0;
struct node *creat()
{
struct node *p;
p=(struct node *)malloc(sizeof(struct node));
int i;
for(i=0;i<26;i++)
{
p->next[i]=NULL;
}
p->flag=0;
return p;
};
void inser()
{
int len,i,j;
struct node *p,*q;
len=strlen(str);
p=(struct node *)malloc(sizeof(struct node));
p=head;
for(i=0; i<len; i++)
{
int t=str[i]-'a';
if(p->next[t]==NULL)
{
p->next[t]=creat();
}
p=p->next[t];
}
p->flag++;
if(ma<p->flag)
ma=p->flag;
};
int main()
{
int n,i;
head=(struct node *)malloc(sizeof(struct node));
for(i=0; i<26; i++)
head->next[i]=NULL;
head->flag=0;
scanf("%d",&n);
getchar();
while(n--)
{
scanf("%s",str);
inser();
}
printf("%d\n",ma);
return 0;
}
浙公网安备 33010602011771号