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

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

ZJCPC2004

<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>

时间: 2024-12-19 04:16:23

hdoj 1251 统计难题(经典字典树)的相关文章

[ACM] hdu 1251 统计难题 (字典树)

统计难题 Problem Description Ignatius近期遇到一个难题,老师交给他非常多单词(仅仅有小写字母组成,不会有反复的单词出现),如今老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Input 输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每一个提问都是一个字符串. 注意:本题仅仅有一组測试数据,处理到文件结束. Out

HDU 1251 统计难题(字典树 裸题 链表做法)

Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Input 输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串. 注意:本题只有一组测试数据,处理到文件结束. Output 对于每个提

HDU 1251 统计难题(字典树模板题 || map运用)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Input 输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一

hdu 1251 统计难题(字典树)

Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Input 输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串. 注意:本题只有一组测试数据,处理到文件结束. Output 对于每个提

HDU 1251 统计难题 (字典树)(查询是否为前缀)

统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submission(s): 37626    Accepted Submission(s): 13858 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的

HDU 1251 统计难题(字典树入门模板题 很重要)

统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submission(s): 56382    Accepted Submission(s): 19709 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的

HDU 1251 统计难题(字典树模板题)

http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意:给出一些单词,然后有多次询问,每次输出以该单词为前缀的单词的数量. 思路: 字典树入门题. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 const int maxn = 1000005; 6 7 int num = 0; 8 9 struct Tr

HDU 1251 统计难题【字典树】

题意:中文题--跟着模板敲的--第一棵字典树[email protected][email protected] 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 typedef struct node 7 { 8 int cnt; 9 struct node *next[26]; 10 } Tr

poj 1251 统计难题(字典树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 AC代码: 1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<cstring> 5 #include<queue> 6 #include<string> 7 #include<cmath> 8 using namespace

统计难题(简单字典树)

字典树(讲解+模板) 又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优点是:利用字符串的公共前缀来节约存储空间,最大限度地减少无谓的字符串比较,查询效率比哈希表高. 字典树跟字典很相似,当我们要查询一个单词是不是在字典中时,我们首先查询单词的第一个字母,之后确定了一个字母后,查找下一个字母,反复这种操作,直到找到单词,但是这种效率很低,在庞大的单词系统面前,则显得心塞了.