HDU (1004) Let the Balloon Rise

Source: Internet
Author: User

Let the Balloon Rise

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 149425 Accepted Submission (s): 59368

Problem Description

Contest Time again! How excited it was to see balloons floating around. But to tell you a secret, the judges ' favorite time was guessing the most popular problem. When the contest was over, they would count the balloons of each color and find the result.
This is the year that they decide to leave the lovely job to you.

Input

Input contains multiple test cases. Each test case is starts with a number n (0 < N <=) – 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 lower-case letters.

A test Case with N = 0 terminates the-input and this-test case are not-to-be processed.

Output

For each case, print the color of balloon for the most popular problem on a. It is guaranteed this 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

First implementation: Structure comparison:

#include <iostream>#include<string.h>using namespacestd;structnode{Charballoon[ -]; intCount_ball=0;};intMain () {intN; structNode color[1005],temp;  while(Cin >>N) {if(N = =0)             Break; intI, J;  for(i =0; i < N; i++) {cin>>Color[i].balloon; Color[i].count_ball=1; }         for(i =0; i < N; i++)//counts from the current color until all the subsequent colors have been counted, or the number of colors        {             for(j = i +1; J < N; J + +)            {                if(strcmp (Color[i].balloon, color[j].balloon) = =0) {Color[i].count_ball++; }            }        }         for(i =0; I < N-1; i++)//compare to find out the biggest color        {             for(j = i; J < N-1I J + +)            {                if(Color[j].count_ball > Color[j +1].count_ball) {Temp=Color[j]; COLOR[J]= Color[j +1]; Color[j+1] =Temp; } }} cout<<color[i].balloon <<Endl; }        return 0;}

Second way to achieve map:

#include <iostream>#include<string>#include<map>using namespacestd;intMain () {intN; Map<string,int>ballsum; stringstr;  while(Cin >> N&&n >0) {ballsum.clear ();//Clear Map         while(n--) {cin>>str; BALLSUM[STR]++; }        intMax =0; stringMaxclor; Map<string,int>::iterator ITER;//Defining iterators         for(iter = Ballsum.begin (); ITER! = Ballsum.end (); iter++)//Iterative Lookups        {            if(Iter->second > Max)//find the maximum number of colors{Max= (*iter). Second; Maxclor= (*iter). First;//assign the color value in map to Maxcolor}} cout<< Maxclor <<Endl; }    return 0;}

HDU (1004) Let the Balloon Rise

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.