Let the Balloon Rise
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 107610 Accepted Submission(s): 41752
Total Submission(s): 107610 Accepted Submission(s): 41752
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.
This year, they decide to leave this lovely job to you.
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.
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
Author
WU, Jiazhi
Source
//map函数的应用
#include<iostream>
#include<string>
#include<map>
using namespace std;
map<string, int>vis;
int main()
{
int n;
int max = -1;
string t;
string s;
scanf("%d", &n);
while (n != 0)
{
vis.clear();
max = -1;
while (n--)
{
cin >> s;
vis[s]++;
if (vis[s]>max)
{
max = vis[s];
t = s;
}
#include<string>
#include<map>
using namespace std;
map<string, int>vis;
int main()
{
int n;
int max = -1;
string t;
string s;
scanf("%d", &n);
while (n != 0)
{
vis.clear();
max = -1;
while (n--)
{
cin >> s;
vis[s]++;
if (vis[s]>max)
{
max = vis[s];
t = s;
}
}
cout <<t<< endl;
cin >> n;
}
return 0;
}
cout <<t<< endl;
cin >> n;
}
return 0;
}
//二叉排序树
#include<stdio.h>
#include<iostream>
#include<string>
using namespace std;
string s;
int Max = -1;
struct node
{
#include<stdio.h>
#include<iostream>
#include<string>
using namespace std;
string s;
int Max = -1;
struct node
{
node() {}
node(string A) { this->a = A;b = 1; lchild = NULL;rchild = NULL; }
string a;
int b;
node *lchild;
node *rchild;
node(string A) { this->a = A;b = 1; lchild = NULL;rchild = NULL; }
string a;
int b;
node *lchild;
node *rchild;
};
void inorder(node *T)
{
if (T == NULL) return;
if ((T->b>Max) || (T->b == Max&&T->a<s))
{
Max = T->b;
s = T->a;
void inorder(node *T)
{
if (T == NULL) return;
if ((T->b>Max) || (T->b == Max&&T->a<s))
{
Max = T->b;
s = T->a;
}
inorder(T->lchild);
inorder(T->rchild);
inorder(T->lchild);
inorder(T->rchild);
}
void insert(node *&T, string c)
{
if (T == NULL)
{
T = new node(c);
return;
}
if (T->a<c)
{
insert(T->rchild, c);
}
else if (T->a>c)
{
insert(T->lchild, c);
}
else
{
T->b++;
void insert(node *&T, string c)
{
if (T == NULL)
{
T = new node(c);
return;
}
if (T->a<c)
{
insert(T->rchild, c);
}
else if (T->a>c)
{
insert(T->lchild, c);
}
else
{
T->b++;
}
}
int main()
{
int n;
string p;
cin >> n;
while (n != 0)
{
Max = -1;
cin >> p;
s = p;
node *T = new node(p);
--n;
while (n--)
{
cin >> p;
insert(T, p);
}
inorder(T);
cout << s << endl;
cin >> n;
inorder(T);
cout << s << endl;
cin >> n;
}
return 0;
}
}
//边建立节点边操作
#include<stdio.h>
#include<iostream>
#include<string>
using namespace std;
string s;
int Max = -1;
struct node
{
#include<stdio.h>
#include<iostream>
#include<string>
using namespace std;
string s;
int Max = -1;
struct node
{
node() {}
node(string A) { this->a = A;b = 1; lchild = NULL;rchild = NULL; }
string a;
int b;
node *lchild;
node *rchild;
node(string A) { this->a = A;b = 1; lchild = NULL;rchild = NULL; }
string a;
int b;
node *lchild;
node *rchild;
};
void insert(node *&T, string c)//插入操作
{
if (T == NULL)
{
T = new node(c);
return;
}
if (T->a<c)
{
insert(T->rchild, c);
}
else if (T->a>c)
{
insert(T->lchild, c);
}
else
{
T->b++;
if ((Max < T->b)||(Max==T->b)&&T->a<s)
{
Max = T->b;
s = T->a;
}
{
if (T == NULL)
{
T = new node(c);
return;
}
if (T->a<c)
{
insert(T->rchild, c);
}
else if (T->a>c)
{
insert(T->lchild, c);
}
else
{
T->b++;
if ((Max < T->b)||(Max==T->b)&&T->a<s)
{
Max = T->b;
s = T->a;
}
}
}
int main()
{
int n;
string p;
cin >> n;
while (n != 0)
{
Max = -1;
cin >> p;
s = p;
node *T = new node(p);
--n;
while (n--)
{
cin >> p;
insert(T, p);
}
cout << s << endl;
cin >> n;
cout << s << endl;
cin >> n;
}
return 0;
}
}
//散列表函数的应用
#include <bits/stdc++.h>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
int c[10000];
string name[10000];
unsigned int hashf(const char *str)
{
register unsigned int h;
register unsigned char *p;
for(h=0, p = (unsigned char *)str; *p ; p++)
h = 31 * h + *p;
return h%10000;
}
main ()
{
int n;
string s;
while(cin>>n)
{
memset((void *)c,0,sizeof(c));
while(n--)
{
cin>>s;
if(name[hashf(s.c_str())]=="")
name[hashf(s.c_str())]=s;
c[hashf(s.c_str())]++;
}
int maxi=0;
for(int i=0; i<sizeof(c)/sizeof(int); i++)
{
if(c[maxi]<c[i]) maxi=i;
}
cout<<name[maxi]<<endl;
}
return 0;
}
#include<stdlib.h>
#include<stdio.h>
using namespace std;
int c[10000];
string name[10000];
unsigned int hashf(const char *str)
{
register unsigned int h;
register unsigned char *p;
for(h=0, p = (unsigned char *)str; *p ; p++)
h = 31 * h + *p;
return h%10000;
}
main ()
{
int n;
string s;
while(cin>>n)
{
memset((void *)c,0,sizeof(c));
while(n--)
{
cin>>s;
if(name[hashf(s.c_str())]=="")
name[hashf(s.c_str())]=s;
c[hashf(s.c_str())]++;
}
int maxi=0;
for(int i=0; i<sizeof(c)/sizeof(int); i++)
{
if(c[maxi]<c[i]) maxi=i;
}
cout<<name[maxi]<<endl;
}
return 0;
}
#include<stdio.h>
#include<string.h>
struct node
{
int cnt;
node *next[26];
node()
{
cnt=0;
for(int i=0;i<26;i++)
next[i]=NULL;
}
}*trie,p;
int cnt;
char res[100];
int max;
void insert(char word[])
{
trie=&p;
for(int i=0;word[i]!='\0';i++)
{
int t=word[i]-'a';
if(trie->next[t]==NULL)
{
trie->next[t]=new node;
}
trie=trie->next[t];
}
trie->cnt++;
if(trie->cnt>max)
{
max=trie->cnt;
strcpy(res,word);
}
}
void init()
{
trie=&p;
for(int i=0;i<26;i++)
{
trie->next[i]=NULL;
trie->cnt=0;
}
}
int main()
{
int n;
char word[20];
while(scanf("%d",&n),n)
{
memset(word,0,sizeof(word));
cnt=0;
max=0;
getchar();
for(int i=0;i<n;i++)
{
scanf("%s",word);
insert(word);
}
printf("%s\n",res);
}
return 0;
}
#include<string.h>
struct node
{
int cnt;
node *next[26];
node()
{
cnt=0;
for(int i=0;i<26;i++)
next[i]=NULL;
}
}*trie,p;
int cnt;
char res[100];
int max;
void insert(char word[])
{
trie=&p;
for(int i=0;word[i]!='\0';i++)
{
int t=word[i]-'a';
if(trie->next[t]==NULL)
{
trie->next[t]=new node;
}
trie=trie->next[t];
}
trie->cnt++;
if(trie->cnt>max)
{
max=trie->cnt;
strcpy(res,word);
}
}
void init()
{
trie=&p;
for(int i=0;i<26;i++)
{
trie->next[i]=NULL;
trie->cnt=0;
}
}
int main()
{
int n;
char word[20];
while(scanf("%d",&n),n)
{
memset(word,0,sizeof(word));
cnt=0;
max=0;
getchar();
for(int i=0;i<n;i++)
{
scanf("%s",word);
insert(word);
}
printf("%s\n",res);
}
return 0;
}