• HDU 1004 Let the Balloon Rise


    字典树(Trie)


    题意是说找出最多的颜色。也就是找出出现次数最多的单词。

    插入,然后遍历一遍把最多的输出就能够。


    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<queue>
    #include<algorithm>
    #include<map>
    #include<stack>
    #include<iostream>
    #include<list>
    #include<set>
    #include<cmath>
    #define INF 0x7fffffff
    #define eps 1e-6
    #define LL long long
    using namespace std;
    
    struct Trie
    {
        int word[1000][26];
        int sz,cot;
        int ex[1000*10];
        char love[10010][16];
        void intTrie()
        {
            sz=1;
            cot=1;
            memset(word,0,sizeof(word));
            memset(ex,0,sizeof(ex));
        }
        void insert(char *s)
        {
            int u=0,c,len=strlen(s);
            for(int i=0; i<len; i++)
            {
                c=s[i]-'a';
                if(!word[u][c])
                    word[u][c]=sz++;
                u=word[u][c];
            }
            ex[u]++;
            strcpy(love[u],s);
            //return ex[u];
        }
        void fina()
        {
            int m=0;
            char tmp[16];
            for(int i=0;i<10010;i++)
            {
                if(ex[i]>m)
                {
                    m=ex[i];
                    strcpy(tmp,love[i]);
                }
            }
            puts(tmp);
        }
    }wo;
    
    int main()
    {
        int n;
        while(scanf("%d",&n),n)
        {
            char str[16];
            int cot=0;
            wo.intTrie();
            for(int i=0;i<n;i++)
            {
                scanf("%s",str);
                wo.insert(str);
            }
            wo.fina();
        }
    }
    


  • 相关阅读:
    pandas isin 和not in
    游戏开发需要学什么?
    打开页面,数字会自增的效果怎么弄?
    jq 导航栏点击添加/删除类(a标签跳转页面)
    bootstrap+jq分页
    2020/12/18
    2020/12/17
    2020/12/16
    2020/12/15
    2020/12/14
  • 原文地址:https://www.cnblogs.com/claireyuancy/p/6817594.html
Copyright © 2020-2023  润新知