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;
    }
     
  • 相关阅读:
    模拟死锁
    B站学习斯坦福大学Swift 语言教程 iOS11 开发【第一集】踩到的几个坑(XCode 13.2.1版本)
    数学之美番外篇:平凡而又神奇的贝叶斯方法
    joj 1753: Street Numbers
    二叉树的三种遍历(递归+非递归)
    joj 1905: Freckles
    joj 2630: A Pair of Graphs(同构图的判定)
    vue3.x 中获取dom元素
    defineProperty 和 Proxy 的区别
    vue 按钮的防抖和节流
  • 原文地址:https://www.cnblogs.com/jhz033/p/5581141.html
Copyright © 2011-2022 走看看