码迷,mamicode.com
首页 > 其他好文 > 详细

hdoj 1251 统计难题(经典字典树)

时间:2015-04-18 11:34:20      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:acm   算法   杭电   数据结构   c   

Let the Balloon Rise


技术分享
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 84414    Accepted Submission(s): 31834


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
 
<span style="font-size:14px;">#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<stack>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
#define INF 0x7fffffff;</span>
<span style="font-size:14px;">//字典树:
<strong><span style="color:#ff0000;">struct node
{
    int x;
    node *next[26];
    node()
    {
        x=1;
        memset(next,0,sizeof(next));
    }
};
node *root=NULL;

void insert(char *s)
{
    node *p=root;
    int i,k;
    for(i=0;i<strlen(s);i++)
    {
      k=s[i]-'a';
      if(p->next[k]==NULL)
          p->next[k]=new node;
      else
          p->next[k]->x++;
      p=p->next[k];
    }
}

int search(char *s)
{
    node *p=root;
    int i,k;
    for(i=0;i<strlen(s);i++)
    {
        k=s[i]-'a';
        if(p->next[k]==NULL)
         return 0;
          p=p->next[k];
    }
  return p->x;
}</span></strong>

char str1[50],str2[50];
int main()
{  
    <strong><span style="color:#ff0000;">root=new node;//注意之前一定要建树</span></strong>
	while(gets(str1)&&strlen(str1))//注意此处的输入控制
		insert(str1);
	while(gets(str2)!=NULL)
	{
	  int temp=search(str2);
	  printf("%d\n",temp);	
	} 
    return 0;
}</span>


hdoj 1251 统计难题(经典字典树)

标签:acm   算法   杭电   数据结构   c   

原文地址:http://blog.csdn.net/lh__huahuan/article/details/45111307

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!