hdoj 1004 Let the Balloon Rise(模拟 || 字典树)

Let the Balloon Rise

http://acm.hdu.edu.cn/showproblem.php?pid=1004

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

Total Submission(s): 84401    Accepted Submission(s): 31831

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 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

Author

WU, Jiazhi

Source

ZJCPC2004

AC1字典树:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<stack>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
#define INF 0x7fffffff;
struct node
{
    int x;
    node *next[26];
    node()
    {
        x=0;
        memset(next,0,sizeof(next));
    }
};
node *root=NULL;

void insert(char *s)
{
    node *p=root;
    int i,k;
    for(i=0;i<strlen(s);i++)
    {
      k=s[i]-'a';
      if(p->next[k]==NULL)
          p->next[k]=new node;
      else
          p->next[k]->x++;
      p=p->next[k];
    }
}

int search(char *s)
{
    node *p=root;
    int i,k;
    for(i=0;i<strlen(s);i++)
    {
        k=s[i]-'a';
        if(p->next[k]==NULL)
         return 0;
          p=p->next[k];
    }
  return p->x;
}

char str[1010][30];
int main()
{

    int n;
    root=new node;
    while(scanf("%d",&n),n)
    {
        int i=0;
        memset(str,0,sizeof(str));
        for(i=0;i<n;i++)
        {
          scanf("%s",str[i]);
          insert(str[i]);
        }

       int ma=-INF;
       int j=0;
       for(i=0;i<n;i++)
       {
        int k=search(str[i]);
           if(k>ma)
           {
            ma=k;
         j=i;
        }

       }
       printf("%s\n",str[j]);
    }
    return 0;
}

AC2 模拟:

#include<cstdio>
#include<string.h>
#include<stdlib.h>
const int max= 1000+10;
char ba[max][20];
int b[max];

int main()
{
    int N;
    while(scanf("%d",&N),N)
    {
        int i,j;

        memset(ba,0,sizeof(ba));
        getchar();
        for(i=0;i<N;i++)
          scanf("%s",ba[i]);

         memset(b,0,sizeof(b));
          for(i=0;i<N;i++)
              for(j=i+1;j<N;j++)
                 if(strcmp(ba[i],ba[j])==0)
                   b[i]++;

            int m=0,t;
            for(j=0;j<N;j++)
                if(m<=b[j])
                  m=b[j];

            for(i=0;i<N;i++)
                  if(b[i]==m)
                        {
                      printf("%s\n",ba[i]);
                        break;
                    }

    }
    system("pause");
    return 0;
}
时间: 2024-12-10 02:51:20

hdoj 1004 Let the Balloon Rise(模拟 || 字典树)的相关文章

HDOJ 1004 - Let the Balloon Rise(让气球飞起来)

Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 138223    Accepted Submission(s): 54591 Problem Description Contest time again! How excited it is to see balloons floating ar

HDOJ 1004 Let the Balloon Rise (字符串+stl)

题目: 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 co

HDU 1004 Let the Balloon Rise【STL&lt;map&gt;】

Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 123800    Accepted Submission(s): 48826 Problem Description Contest time again! How excited it is to see balloons floating ar

hdu 1004 Let the Balloon Rise

Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 80469    Accepted Submission(s): 30293 Problem Description Contest time again! How excited it is to see balloons floating aro

杭电1004 Let the Balloon Rise

Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 120507    Accepted Submission(s): 47270 Problem Description Contest time again! How excited it is to see balloons floating ar

HDU 1004 Let the Balloon Rise map

Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 104108    Accepted Submission(s): 40046 Problem Description Contest time again! How excited it is to see balloons floating ar

HDU 1004 Let the Balloon Rise (map使用)

分析:题意简单,就用map的知识即可 #include <stdio.h> #include <iostream> #include <string.h> #include <map> using namespace std; int main() { int i,a,b,k; int N; char num[10]; while (~scanf("%d",&N)) { map<string,int> mp; whil

UVA-12333 Revenge of Fibonacci(竖式加法模拟 &amp; 字典树)

题目: 给出一个斐波那契数字的前缀,问第一个有这个前缀的数字在斐波那契数列中是第几个. 思路: 紫书提示:本题有一定效率要求.如果高精度代码比较慢,可能会超时. 利用滚动数组和竖式加法来模拟斐波那契相加的过程,在这个过程中每得出一个斐波那契数字就用字典树存一下. PS:在滚动数组中存的斐波那契数字是逆序存储的. 代码: #include <bits/stdc++.h> #define inf 0x3f3f3f3f #define MAX 1e9; #define FRE() freopen(&

水题/hdu 1004 Let the Balloon Rise

题意 给出n个字符串,输出出现次数最多的那个 分析 存下字符串后排序,再统计,输出 Accepted Code 1 /* 2 PROBLEM:hdu1004 3 AUTHER:Nicole Lam 4 MEMO:水题 5 */ 6 7 #include<iostream> 8 #include<cstring> 9 #include<string> 10 #include<algorithm> 11 using namespace std; 12 13 in