"Floating Balloon (ID1004)" Problem solving report of Hangzhou Judge Online

Source: Internet
Author: User
Tags array length

/*
"Floating Balloon (ID1004)" Problem solving report of Hangzhou Judge Online
Qiao if clumsy (welcome reprint, but please specify Source: Http://blog.csdn.net/qiaoruozhuo)


Title Description:
Let the Balloon Rise
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


Author
WU, Jiazhi


Source
ZJCPC2004


Topic Analysis:
A lame translation:
Floating balloons.
The game has started again. The most exciting thing is to watch the balloon rise slowly. Tell you a secret, the judges are most interested in guessing which color balloons are most popular. When the game is over, they calculate the number of balloons for each color and choose the most numerous balloon colors. This year, they are going to give you this interesting job.
Input
The input contains multiple sets 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, and the color of the balloon is represented by a string of no more than 15 lowercase letters.
The test case ends the input with n =0, and the test case is not processed.
Output
For each set of test data, output the hottest balloon color. Ensure that each set of test data is resolved.


Algorithm Analysis:
The algorithm is simple, it is a basic application of reading strings and comparing size. A data structure is a structure that contains the color and number of balloons. To simplify the code and speed up, I set the structure array with the balloon information stored as a global variable.
In order to improve the efficiency of the search, I arranged the array according to the color string size, using binary lookup method. First find the insertion position, if it is an existing color balloon, just increase the number of the color balloon, or insert a new node. Finally, iterate through the array to find the most numerous balloons and output their colors.
The highlight of the program is the binary insertion algorithm, which is also the most error-prone place.
Description
Algorithm idea: Find and insert.
Data structure: An array of structures that contains a string and an integer data.
Time complexity: O (Nloglen), where n is the number of balloons, Len is the total number of balloon colors
Spatial complexity: O (N);


The code is as follows:


References: summary of various insertion sorting algorithms (clumsy) http://blog.csdn.net/qiaoruozhuo/article/details/39099953
*/
#include <stdio.h>
#include <stdlib.h>


#define MAX 1010


struct node{
Char col[16];//balloon Color
int num;//Balloon Number
} Ball[max];


int Getmax (int n);
int Insert (char *str, int n);//binary Insert new node


int main (void)
{
Char str[16];
int I, Len, N, pos;

scanf ("%d", &n);
while (n! = 0)
{
len = 0; Array length zeroing
for (i=0; i<n; i++)
{
scanf ("%s", str);
len = insert (str, len);//Insert new node and return array length
}
pos = Getmax (len); Returns the largest number of balloon balloons
Puts (Ball[pos].col);
scanf ("%d", &n);
}


return 0;
}


int Getmax (int n)//Find the largest number of balloon balloons
{
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)//binary Insert new node
{
int i, flag = 1;//flag 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)//binary Find insertion position
{
Mid = (low + high)/2;
Flag = strcmp (str, ball[mid].col); Comparing string sizes
if (flag = = 0)//Existing color node
Break
else if (Flag < 0)
High = mid-1;
Else
Low = mid + 1;
}

if (flag = = 0)//increase the number of existing color balloons
ball[mid].num++;
else//Insert 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; Returns the new array length
}

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.