zoukankan      html  css  js  c++  java
  • hdu 1004 Let the Balloon Rise(字典树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1004

    Let the Balloon Rise

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 90644    Accepted Submission(s): 34459


    Problem Description
    Contest time again! How excited it is to see balloons floating around. But to tell you a secret, the judges' favorite time is guessing the most popular problem. When the contest is over, they will count the balloons of each color and find the result.

    This year, they decide to leave this lovely job to you.
     
    Input
    Input contains multiple test cases. Each test case starts with a number N (0 < N <= 1000) -- the total number of balloons distributed. The next N lines contain one color each. The color of a balloon is a string of up to 15 lower-case letters.

    A test case with N = 0 terminates the input and this test case is not to be processed.
     
    Output
    For each case, print the color of balloon for the most popular problem on a single line. It is guaranteed that there is a unique solution for each test case.
     
    Sample Input
    5
    green
    red
    blue
    red
    red
    3
    pink
    orange
    pink
    0
     
    Sample Output
    red
    pink
     
    Author
    WU, Jiazhi
     
    题目大意:输出出现次数最多的字符串。
    解题思路:每次建一条树就在结尾的时候标记一下次数,找到最大的最后输出对应的字符串就可以了。
     
    详见代码。
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 
     5 using namespace std;
     6 
     7 struct node
     8 {
     9     node *next[26];
    10     int Count;
    11     node()
    12     {
    13         for (int i=0; i<26; i++)
    14             next[i]=NULL;
    15         Count=0;
    16     }
    17 };
    18 
    19 char cc[5000];
    20 node *p,*root=new node();
    21 void insert(char *s)
    22 {
    23     p=root;
    24     for (int i=0; s[i]; i++)
    25     {
    26         int k=s[i]-'a';
    27         if (p->next[k]==NULL)
    28             p->next[k]=new node();
    29         p=p->next[k];
    30     }
    31     p->Count++;
    32 }
    33 
    34 int Search(char *s)
    35 {
    36     p=root;
    37     for (int i=0; s[i]; i++)
    38     {
    39         int k=s[i]-'a';
    40         if (p->next[k]==NULL)
    41             return 0;
    42         p=p->next[k];
    43     }
    44     //cout<<p->Count<<" "<<"3333333333"<<endl;
    45     return p->Count;
    46 }
    47 
    48 int main()
    49 {
    50     int t;
    51     char ch[1010];
    52     int Max;
    53     while (~scanf("%d",&t))
    54     {
    55         Max=0;
    56         if (t==0)
    57             break;
    58         while (t--)
    59         {
    60             scanf("%s",ch);
    61             //gets(ch);
    62             insert(ch);
    63             int ans=Search(ch);
    64             if (ans>Max)
    65             {
    66                 Max=ans;
    67                 strcpy(cc,ch);
    68             }
    69         }
    70         printf ("%s
    ",cc);
    71     }
    72     return 0;
    73 }
  • 相关阅读:
    探寻京东云核心竞争力的源泉
    在线公开课 | 教你如何自行搭建一个威胁感知大脑?
    推迟学习系统调用
    linux中未实现的系统调用
    man(2) V
    man(2) W
    图像叠加
    man(2) readv writev
    qt 视频播放
    ffmpeg参数
  • 原文地址:https://www.cnblogs.com/qq-star/p/4750928.html
Copyright © 2011-2022 走看看