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 <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <queue> #include <stack> #include <cmath> #include <map> using namespace std; //char s[100000][6]; struct node { int ans; struct node *next[26]; }; struct node *creat() { int i; struct node *p; p=(struct node *)malloc(sizeof(struct node )); for(i=0;i<26;i++) { p->next[i]=NULL; } p->ans=NULL; return p; } void Insert(struct node *&root,char *s,int &Max) { if(root==NULL) { root=creat(); } if(s[0]!='\0') { Insert(root->next[s[0]-'a'],s+1,Max); } else { root->ans++; Max=max(root->ans,Max); } } int main() { struct node *root=NULL; int n,Max=1,i,j; char s[6]; while(scanf("%d",&n)!=EOF) { for(i=0;i<n;i++) { scanf("%s",s); Insert(root,s,Max); } printf("%d\n",Max); } return 0; } /************************************** Problem id : SDUT OJ 2892 User name : mxjr130326刘继国 Result : Accepted Take Memory : 4508K Take Time : 20MS Submit Time : 2014-06-28 21:38:58 **************************************/
字典树是个很神奇的东西,建树,插入,查找,释放空间