zoukankan      html  css  js  c++  java
  • nyoj290 动物统计加强版

     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
  • 相关阅读:
    笔记-树形dp
    20181018 考试记录
    20181015 考试记录&数论
    [模板]2-SAT 问题&和平委员会
    FLask的偏函数应用
    Flask中的g到底是个什么鬼?
    Flask源码关于local的实现
    Flask的“中间件”
    Flask之模板
    FLask之视图
  • 原文地址:https://www.cnblogs.com/shihuajie/p/2629025.html
Copyright © 2011-2022 走看看