hdoj1004 let the ballon rise


Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

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 numberN (0 < N <= 1000)— the total number of balloons distributed. The nextNlines contain one color each. The color of a balloon is a string of up to15lower-case letters.

A test case withN = 0terminates 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

1
2
3
4
5
6
7
8
9
10
11
5
green
red
blue
red
red
3
pink
orange
pink
0

Sample Output

1
2
red
pink

Source

ZJCPC2004

Code

方法一

使用map容器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

using namespace std;

int ()
{
#ifdef usefre
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif

int n;
while(cin>>n&&n)
{
map<string,int> elements;
while(n--)
{
string tmp;
cin>>tmp;
if(elements.find(tmp)!=elements.end())
{
elements[tmp]++;
}
else
{
elements[tmp]=1;
}
}
map<string,int>::iterator it,ittmp;
int max=-1000;
for(it=elements.begin();it!=elements.end();it++)
{
if((*it).second>max)
{
max=(*it).second;
ittmp=it;
}
}
cout<<(*ittmp).first<<endl;
}
return 0;
}

方法二

使用结构体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

using namespace std;

typedef struct
{
string color;
int count;
} ballon;
int ()
{
#ifdef usefre
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int n;
while (cin >> n && n)
{
ballon b[1005];
int num = 0;
while (n--)
{
string tmp;
cin >> tmp;
int isNewColor = 1;
for (int i = 0; i < num; i++)
{
if (tmp == b[i].color)
{
b[i].count++;
isNewColor = 0;
break;
}
}
if (isNewColor)
{
b[num].color = tmp;
b[num].count = 1;
num++;
}
}
int p_max, max = 0;
for (int i = 0; i < num; i++)
{
if (b[i].count > max)
{
max = b[i].count;
p_max = i;
}
}
cout << b[p_max].color << endl;
}
return 0;
}