Let the Balloon 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.
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
题意:找出出现次数最多的那个字符串;
strcmp:
#include<stdio.h> #include<string.h> char a[1000][20]; int b[1011]; int main () { int x,y,z,j,k,i,t,max; while(scanf("%d",&x)!=EOF) { if(x==0) break; getchar(); memset(b,0,sizeof(b)); for(i=0;i<x;i++) scanf("%s",a[i]); for(i=0;i<x;i++) for(t=0;t<x;t++) if(strcmp(a[i],a[t])==0) b[i]++; max=b[0]; k=0; for(i=1;i<x;i++) if(max<b[i]) {max=b[i];k=i;} printf("%s ",a[k]); } return 0; }
map:
#include<stdio.h> #include<map> #include<iostream> #include<string.h> #include<string> using namespace std; int main() { int x,y,z,i,t,max; string a,b; map<string,int>p; while(scanf("%d",&x)!=EOF) { getchar(); p.clear(); if(x==0) break; max=0; for(i=0;i<x;i++) { cin>>a; p[a]++; if(p[a]>max) { max=p[a]; b=a;} } cout<<b<<endl; } return 0; }
trie树:
#include<iostream> #include<cstdio> #include<cmath> #include<string> #include<queue> #include<algorithm> #include<stack> #include<cstring> #include<vector> #include<list> #include<set> #include<map> using namespace std; #define ll long long #define mod 1000000007 #define pi (4*atan(1.0)) const int N=1e5+10,M=5e6+10,inf=1e9+10; int a[N][27],sum[M],len,ans=0; void init() { memset(a,0,sizeof(a)); memset(sum,0,sizeof(sum)); len=1; ans=0; } char aaa[N]; int getnum(char a) { return a-'a'; } void insertt(char *aa) { int u=0,n=strlen(aa); for(int i=0; i<n; i++) { int num=getnum(aa[i]); if(!a[u][num]) { a[u][num]=len++; } u=a[u][num]; sum[u]++; } if(sum[u]>ans) { ans=sum[u]; strcpy(aaa,aa); } } int getans(char *aa) { int u=0,x=strlen(aa); for(int i=0; i<x; i++) { int num=getnum(aa[i]); if(!a[u][num]) return 0; u=a[u][num]; } return sum[u]; } char ch[N]; int main() { int x,y,z,i,t; while(~scanf("%d",&x)) { if(x==0)break; init(); for(i=0; i<x; i++) { scanf("%s",ch); insertt(ch); } printf("%s ",aaa); } return 0; }