字典树

Let the Balloon Rise

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


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
 

Source
 

Recommend
JGShining
 

Statistic |  Submit |  Discuss |  Note
Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2011 HDU ACM Team. All Rights Reserved.
Designer & Developer Wang Rongtao LinLe GaoJie GanLu
Total 0.000377(s) query 1, Server time : 2011-08-03 14:51:21, Gzip enabled

 

 

这题是最基本的字典树,代码ac了。但是写的很乱

 

 

#include<iostream>
#include<string>
using namespace std;
int imax = 0 ;
char maxs[16];

struct word
{
       word*next[26];
       bool end ;
       int num ;
};

void creat(word* head , char w[])
{
     word*p = head ;
     for(int i =0 ; w[i] ; i++)
     {
             int n = w[i] - 'a';
             if(p->next[n] == NULL)
             {
                           p->next[n] = new word;
                           for(int i = 0; i < 26 ; i++)
                           {
                                   p->next[n]->next[i] = NULL;
                           }
                           p->next[n]->end = false ;
                           p->next[n]->num = 0 ;
             }
             
             p = p->next[n];     
     }
     p->num ++ ;
     p->end = true ;
}

void search(word *p , char w[] ,int L = -1 ,int num = -1)
{
     if(num != -1)
     {
         w[L] = char(num+'a');
         w[L+1] = 0 ;  
     }
     if(p->end)
     {
         if(p->num >imax)
         {
                   imax = p->num ;
                   strcpy(maxs,w);
         }
     }
     for(int i = 0 ; i<26 ; i++)
     {
             if(p->next[i]!= NULL)
             {
                             search(p->next[i] , w ,L+1 ,i);
             }
     }
}




int main()
{
    int n = 0 ;
    while(cin >>n)
    {
              if(n==0)
              break;
              imax = 0 ;
              word*head = new word;
              for(int i = 0 ; i < 26 ; i++)
              {
                      head->next[i] = NULL;
              }
              head ->num = 0 ;
              head ->end = false ; 
              
              char str[16];
              for(int j = 0 ; j < n ; j++)
              {
                      cin>>str ;
                      creat(head , str);
              }
              
              char maxstr[16];
              search(head , maxstr , -1 , -1);
              cout<<maxs<<endl;
    
    }
}
 

你可能感兴趣的:(算法)