5 aba abb w aba z
2
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
const int N = 20010;
using namespace std;
struct node
{
int flag;
node *next[26];
};
int n,m,ans = 0;
struct node *Creat()
{
node *p = new node;
for(int i=0; i<26; i++ )
{
p->next[i] = NULL;
}
p->flag = 0;
return p;
}
void INsert( node *root, char *b)
{
int num;
int len = strlen(b);
node *p = root;
for(int i = 0;i<len;i++)
{
num = b[i]-‘a‘;
if(p->next[num]==NULL)
{
p->next[num] = Creat();
}
p = p->next[num];
}
p->flag++;
if(p->flag > ans)
ans = p->flag;
}
void FREE(struct node*root)
{
for(int i = 0;i<n;i++)
{
if(root->next[i]!=NULL)
{
FREE(root->next[i]);
}
}
free(root);
}
int main()
{
char a[N][50],s[50];
node *p;
scanf("%d",&m);
p = Creat();
for(int i = 0;i<m;i++)
{
scanf("%s",s);
INsert(p,s);
}
printf("%d\n",ans);
FREE(p);
return 0;
}
原文:http://www.cnblogs.com/yspworld/p/3885806.html