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 <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;
}/**************************************Result : Accepted Take Memory : 4452K Take Time : 10MS Submit Time : 2014-06-21 16:13:31
**************************************/