题目大意
输入一堆字符串,求出现次数最多的一个
分析
字典树例题
在jp的指点下明白
资料传送门
code
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<string>
#include<algorithm>
#include<queue>;
using namespace std;
struct{
int st[150];
int num;
}tree[10000];
int n,m;
string str;
int ans;
int insert(int r,int x,string s)
{
if (x==s.length())
{
tree[r].num++;
if (tree[r].num>ans)
{
ans=tree[r].num;
str=s;
}
return 0;
}
if (!tree[r].st[s[x]])
{
m++;
tree[r].st[s[x]]=m;
}
insert(tree[r].st[s[x]],x+1,s);
}
int main()
{
while ((cin>>n)&&(n))
{
memset(tree,0,sizeof(tree));
ans=0;
m=0;
for (int i=1;i<=n;i++)
{
string s;
cin>>s;
insert(0,0,s);
}
cout<<str<<endl;
}
return 0;
}