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

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<malloc.h>
     4 #define N 26
     5 char s[11];
     6 int max;
     7 
     8 typedef struct Trie{  
     9     Trie *next[N];  //含有26个结点指针(a,b,c.......z)
    10     int count;
    11 }*Node;
    12 
    13 Node root;
    14 
    15 Node creat()
    16 {
    17     Node p = (struct Trie*) malloc(sizeof(struct Trie));
    18     for(int i = 0; i < N; ++i)
    19         p->next[i] = NULL;
    20     p->count = 0;
    21     return p;
    22 }
    23 
    24 void insertNode(char *str)
    25 {
    26     Node p = root;
    27     int i, len = strlen(str);
    28     for(i = 0; i < len; ++i)
    29     {
    30         if(p->next[ str[i] - 'a' ] == NULL)
    31             p->next[ str[i] - 'a' ] = creat();
    32         p = p->next[ str[i] - 'a' ];
    33     }
    34     ++p->count;  //记录该单词出现的个数
    35     if(max < p->count)
    36     {
    37         max = p->count;
    38         strcpy(s,str);
    39     }
    40 }
    41 
    42 int main()
    43 {
    44 //    freopen("in.txt","r",stdin);
    45     int n;
    46     char a[11];
    47     root = creat();
    48     max = 0;
    49     scanf("%d",&n);
    50     while(n--)
    51     {
    52         scanf("%s",a);
    53         insertNode(a);
    54     }
    55     printf("%s %d\n",s,max);
    56     return 0;
    57 }
  • 相关阅读:
    百度面试题
    京东2014年招聘会成都站笔试经历
    把一个字符串的大写字母放到字符串的后面,各个字符的相对位置不变,不能申请额外的空间
    POJ 2234 Matches Game
    POJ 3903 Stock Exchange
    POJ 2853 Sequence Sum Possibilities
    POJ 3519 Minimal Backgammon
    POJ 2096 Collecting Bugs
    POJ 3071 Football
    HDU 1175 连连看
  • 原文地址:https://www.cnblogs.com/yaling/p/3015650.html
Copyright © 2011-2022 走看看