题目链接: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
 
题目大意:输出出现次数最多的字符串。
解题思路:每次建一条树就在结尾的时候标记一下次数,找到最大的最后输出对应的字符串就可以了。
 
详见代码。

 #include <iostream>
#include <cstdio>
#include <cstring> using namespace std; struct node
{
node *next[];
int Count;
node()
{
for (int i=; i<; i++)
next[i]=NULL;
Count=;
}
}; char cc[];
node *p,*root=new node();
void insert(char *s)
{
p=root;
for (int i=; s[i]; i++)
{
int k=s[i]-'a';
if (p->next[k]==NULL)
p->next[k]=new node();
p=p->next[k];
}
p->Count++;
} int Search(char *s)
{
p=root;
for (int i=; s[i]; i++)
{
int k=s[i]-'a';
if (p->next[k]==NULL)
return ;
p=p->next[k];
}
//cout<<p->Count<<" "<<"3333333333"<<endl;
return p->Count;
} int main()
{
int t;
char ch[];
int Max;
while (~scanf("%d",&t))
{
Max=;
if (t==)
break;
while (t--)
{
scanf("%s",ch);
//gets(ch);
insert(ch);
int ans=Search(ch);
if (ans>Max)
{
Max=ans;
strcpy(cc,ch);
}
}
printf ("%s\n",cc);
}
return ;
}

hdu 1004 Let the Balloon Rise(字典树)的更多相关文章

  1. HDU 1004 Let the Balloon Rise(map的使用)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1004 Let the Balloon Rise Time Limit: 2000/1000 MS (J ...

  2. hdu 1004 Let the Balloon Rise strcmp、map、trie树

    Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  3. HDU 1004 Let the Balloon Rise【STL<map>】

    Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  4. hdu 1004 Let the Balloon Rise

    Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  5. HDU 1004 Let the Balloon Rise map

    Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  6. hdu 1004 Let the Balloon Rise 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1004 用STL 中的 Map 写的 #include <iostream> #includ ...

  7. HDU 1004 - Let the Balloon Rise(map 用法样例)

    Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...

  8. HDU 1004 Let the Balloon Rise(map应用)

    Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...

  9. HDU 1004 Let the Balloon Rise(STL初体验之map)

    Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...

随机推荐

  1. 使用 Idea 打 scala程序的 jar 包 - 02

    Artifact ——>+  ——>JAR ——>From modules with dependencies 选择 Module,选择主函数,OK——>OK 勾选Includ ...

  2. 安装Zookeeper出现Unable to start AdminServer,existing abnormally问题解决方法

    问题如下: 出现这个问题主要是由于8080端口占用,可在zoo.cfg中增加admin.serverPort=没有被占用的端口号解决问题.

  3. Java多线程(二) —— 深入剖析ThreadLocal

    对Java多线程中的ThreadLocal类还不是很了解,所以在此总结一下. 主要参考了http://www.cnblogs.com/dolphin0520/p/3920407.html 中的文章. ...

  4. Lottie开源库实现Android动画效果

    Lottie简介 Lottie是一个支持Android.iOS.React Native,并由Adobe After Effects制作aep格式的动画,然后经由bodymovin插件转化渲染为jso ...

  5. P4329 [COCI2006-2007#1] Bond

    题意翻译 有n 个人去执行n 个任务,每个人执行每个任务有不同的成功率,每个人只能执行一个任务,求所有任务都执行的总的成功率. 输入第一行,一个整数n (1≤n≤20 ),表示人数兼任务数.接下来n ...

  6. laravel运行慢是怎么回事?

    因为默认的 hello 页面引用了 google 的字体,至于为什么 google 这么慢就不用我详细说明了吧.

  7. 【bzoj2806】 Ctsc2012—Cheat

    http://www.lydsy.com/JudgeOnline/problem.php?id=2806 (题目链接) 题意 给出M个字符串组成“标准库”.定义L表示将一个字符串分成若干段,每一段的长 ...

  8. 20135239 益西拉姆 linux内核分析 可执行程序的装载

    益西拉姆 + 原创作品请勿转载 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 ” week 7 可 ...

  9. 【数学】【CF1091D】 New Year and the Permutation Concatenation

    Description 给定一个数 \(n\),将所有 \(1~\sim~n\) 的排列按照字典序放到一个序列中,求有多少长度为 \(n\) 的子序列 \(p_i~p_{i+1}~\dots~p_{i ...

  10. TIME_WAIT状态的一些总结

    前言: TCP断开连接的四次握手中, 主动关闭连接的一方的TIME_WAIT状态尤为重要. 1:TCP连接的三次握手和断开的四次挥手 2:由上图可知 在主动关闭的一方, 会经历TIME_WAIT状态, ...