1 #include<cstring>
 2 #include<cstdio>
 3 #include<iostream>
 4 using namespace std;
 5 struct node{
 6     node *next[26];
 7     int count;
 8     node(){      //构造函数,初始化数据 
 9         memset(next,0,sizeof(next));
10         count=0;
11     }
12 };
13 int maxi;   //用来存放最大出现次数 
14 char maxs[11];//存放出现次数最多的字符串 
15 node *root=new node();
16 void insert(char *s)//插入新的字符串 
17 {
18     node *p=root;
19     int i,k;
20     for(i=0;s[i];++i){
21         k=s[i]-'a';
22         if(p->next[k]==NULL) p->next[k]=new node();//不存在此节点则创建 
23         p=p->next[k];   //移向下一结点 
24     }
25     p->count++;//此字符串的出现的次数加一 
26     if(p->count>maxi){//更新maxi和maxs 
27         maxi=p->count;
28         strcpy(maxs,s);
29     }
30 }
31 int main()
32 {
33     int n;
34     char s[11];
35     scanf("%d",&n);
36     while(n--){
37         scanf("%s",s);
38         insert(s);
39     }
40     printf("%s %d\n",maxs,maxi);
41     return 0;
42 }

字典树没什么好说的!!!但时间依然这么大,郁闷!

时间:1544 内存:56408 长度:624

相关文章:

  • 2022-12-23
  • 2021-06-25
  • 2022-02-14
  • 2021-12-20
  • 2021-08-09
  • 2021-08-24
  • 2021-11-30
猜你喜欢
  • 2021-12-03
  • 2021-12-16
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-27
相关资源
相似解决方案