HDU1004 java
·
题目描述
Let the Balloon Rise
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 209576 Accepted Submission(s): 84883
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.
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.
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
思路
这道题的题目意思是找出输入中的字符串中次数最多的一种,我们考虑到要用键值对去存储,键存储颜色,值存储数量。如果本来这个键值对集合有这个键,那么他的值在原来的基础上加1,如果本身这个键值集合没有这个键,那么就把这个键值加入到这个键值集合。最终实现代码的时间复杂度为O(N),空间复杂度为O(k)。
java代码实现

更多推荐



所有评论(0)