题目链接: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. Ubuntu 14.04(64bit)使用mentohust连接校园网

    ubuntu14.04系统安装成功之后,需要连接上网络才可以对更新系统以及安装一些必须包.而在学校中,经常遇到的情况需要通过锐捷客户端来连接校园网. 更新: 在Ubuntu14.04下面不用装ment ...

  2. typescript 不用import?

    如果你想用ts文件,而又不想import,export,怎么办呢? 感谢开发者提供的工具.    https://github.com/domchen/typescript-plus.   感想: 仅 ...

  3. DB磁盘满导致Zabbix Server Crash一例

    故障描述 今天线上zabbix出现几次数据中断的情况,经排查为DB服务器磁盘空间不足导致的.还好我们目前我们zabbix,falcon两套监控系统并存,哈哈. 故障排查过程没什么技术含量,简单的将故障 ...

  4. 【Java并发编程】之五:volatile变量修饰符—意料之外的问题

    volatile用处说明 ​ 在JDK1.2之前,Java的内存模型实现总是从主存(即共享内存)读取变量,是不需要进行特别的注意的.而随着JVM的成熟和优化,现在在多线程环境下volatile关键字的 ...

  5. A Shade of Moonlight CodeForces - 989D

    看这位博主的吧:https://blog.csdn.net/Steaunk/article/details/80660954

  6. [BZOJ2288&BZOJ1150]一类堆+链表+贪心问题

    今天我们来介绍一系列比较经典的堆+链表问题.这类问题的特点是用堆选取最优解,并且通过一些加减操作来实现"反悔". 在看题之前,我们先来介绍一个神器:手写堆. 手写堆的一大好处就是可 ...

  7. SAM

    后缀自动机能识别字符串S的所有子串,是一个DAG. http://blog.csdn.net/huanghongxun/article/details/51112764 https://blog.xe ...

  8. PKUWC Slay The Spire

    题面链接 LOJ sol 好神啊.果然\(dp\)还是做少了,纪录一下现在的思维吧\(QAQ\). 我们首先可以发现期望是骗人的,要不然他乘的是什么xjb玩意. 其实就是要求所有方案的最优方案和. 因 ...

  9. 洛谷 P3698 [CQOI2017]小Q的棋盘 解题报告

    P3698 [CQOI2017]小Q的棋盘 题目描述 小 Q 正在设计一种棋类游戏. 在小 Q 设计的游戏中,棋子可以放在棋盘上的格点中.某些格点之间有连线,棋子只能在有连线的格点之间移动.整个棋盘上 ...

  10. Linux及安全实践三——程序破解

    Linux及安全实践三--程序破解 一.基本知识 常用指令机器码 指令 作用 机器码 nop 无作用(no operation) 90 call 调用子程序,子程序以ret结尾 e8 ret 返回程序 ...