zoukankan      html  css  js  c++  java
  • hdu 1004 Let the Balloon Rise strcmp、map、trie树

    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. 
     
    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
    题意:找出出现次数最多的那个字符串;
    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;
    }
     
  • 相关阅读:
    KEIL5.25生成.bin文件步骤
    【转】树莓派网线直连笔记本电脑
    由编译器指定数组长度带来的一个问题
    【转】C/C++位域结构深入解析
    【转】大小端存储模式精解
    【转】树莓派入门之装系统
    【转】树莓派Raspberry Pi
    stm32的双向io口
    小记之while循环条件的操作位置
    【转】浮点数在计算机中存储方式
  • 原文地址:https://www.cnblogs.com/jhz033/p/5581141.html
Copyright © 2011-2022 走看看