一题简单的C++题

来源:百度知道 编辑:UC知道 时间:2024/05/23 14:39:04
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.

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
呵呵,帮忙写下哈
感觉自己这么想,太麻烦了,看看有没有简单点的方法啊。。。

还可以用hash做
#include<iostream>
using namespace std;
char str[20];
char result[20];
int ans;
bool judge;
struct node
{
int num;
node * child[26];
node();
};
node::node()
{
num=0;
for(int i=0;i<26;i++)
child[i]=NULL;
}
node* Troot;
//*********************************
void insert(char *str, node *root)
{
if(root==NULL)
root=new node;
node* p=root;
int i=0;
while(str[i]!='\0')
{
if(p->child[str[i]-'a']==NULL)
p->child[str[i]-'a']=new node;
p=p->child[str[i]-'a'];
i++;
}
(p->num)++;
if((p->num)>ans)
{
ans=p->num;
judge=true;
}
}
//***************************************************8
void del(node* root)
{
for(int i=0;i<26;i++)
{
if(root->child[i]!=NULL)