Hangzhou University of Electronic Science and Technology Online judge's "floating balloon (id1004)" solution report

Source: Internet
Author: User

Hangzhou University of Electronic Science and Technology Online judge's "floating balloon (id1004)" solution report

Clever if (Welcome to reprint, but please note the Source: http://blog.csdn.net/qiaoruozhuo)

 

Description:

Let the balloon rise

Problem description

Contest time again! How excited It is tosee balloons floating around. but to tell you a secret, the judges 'favoritetime is guessing the most popular problem. when the contest is over, they willcount the balloons of each color and find the result.

This year, they decide to leave this lovelyjob to you.

 

Input

Input contains multiple test cases. eachtest case starts with a number N (0 <n <= 1000) -- The total number ofballoons distributed. the next n lines contain one color each. the color of aballoon is a string of up to 15 lower-case letters.

A test case with n = 0 terminates the inputand this test case is not to be processed.

 

Output

For each case, print the color of balloonfor the most popular problem on a single line. It is guaranteed that there is aunique 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

 

Question Analysis:

Poor translation:

Floating balloon

The competition started again! The most exciting thing is watching the balloon rise slowly. Tell you a secret. The judges are most happy to guess which color of balloon is most popular. When the competition ends, they will calculate the number of balloons in each color and select the most popular balloon colors. This year, they plan to give you this interesting job.

Input

The input contains multiple groups of test data. Each test case starts with an integer N (0 <n <= 1000) -- The total number of balloons. The next n rows output the color of each balloon. The color of the balloon is represented by a string of no more than 15 lower-case letters.

The test case ends with n = 0 and the test case is not processed.

Output

Output the most popular balloon color for each set of test data. Make sure that each group of test data is resolved.

 

Algorithm analysis:

The algorithm for this question is very simple. It is a basic application that reads strings and compares the size. A Data Structure is a structure that contains the color and quantity of balloons. To simplify code and speed up, I set the struct array that stores balloon information as a global variable.

To improve the search efficiency, I sorted the arrays in ascending order by the color string size and used the semi-query method. First, locate the insertion position. If it is an existing color balloon, you only need to increase the number of this color balloon; otherwise, insert a new node. Finally, traverse the array to find the largest number of balloons and output their colors.

The Program highlights the semi-Insertion Algorithm, which is also the most error-prone.

Note:

Algorithm idea: Search and insert.

Data Structure: contains a string and an array of integer data structures.

Time Complexity: O (nloglen). N indicates the number of balloons, and Len indicates the total number of balloons.

Space complexity: O (N );

 

The Code is as follows:

# Include <stdio. h>

# Include <stdlib. h>

 

# Deprecision Max 1010

 

Struct node {

Char Col [16]; // balloon color

Int num; // number of balloons

} Ball [Max];

 

Int getmax (int n );

Int insert (char * STR, int N); // Insert a new node in half

 

Int main (void)

{

Charstr [16];

Inti, Len, N, Pos;

Scanf ("% d", & N );

While (n! = 0)

{

Len = 0; // returns the array length to zero.

For (I = 0; I <n; I ++)

{

Scanf ("% s", STR );

Len = insert (STR, Len); // Insert a new node and return the length of the array.

}

Pos = getmax (LEN); // The maximum number of balloons returned

Puts (ball [POS]. col );

Scanf ("% d", & N );

}

 

Return 0;

}

 

Int getmax (int n) // The maximum number of balloons to be searched

{

Int I, max = 0;

For (I = 0; I <n; I ++)

{

If (ball [I]. Num> ball [Max]. Num)

Max = I;

}

Return Max;

}

 

Int insert (char * STR, int N) // Insert a new node in half

{

Int I, flag = 0; // indicates whether it is an existing color node.

Int mid, low = 0, high = n-1;

If (n = 0)

{

Strcpy (ball [0]. Col, STR );

Ball [0]. num = 1;

Return 1;

}

While (low <= high) // half-lookup insert position

{

Mid = (low + high)/2;

If (strcmp (STR, ball [Mid]. col) = 0)

{

Flag = 1;

Break;

}

Else if (strcmp (STR, ball [Mid]. col) <0)

High = mid-1;

Else

Low = Mid + 1;

}

If (flag = 1) // increase the number of existing color balloons

Ball [Mid]. Num ++;

Else // Insert a new node

{

For (I = N; I> low; I --)

Ball [I] = ball [I-1];

Strcpy (ball [low]. Col, STR );

Ball [low]. num = 1;

N ++;

}

Return N; // return the length of the new array.

}

Hangzhou University of Electronic Science and Technology Online judge's "floating balloon (id1004)" solution report

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.