• hdu4886 TIANKENG’s restaurant(Ⅱ) (trie树或者模拟进制)


    TIANKENG’s restaurant(Ⅱ)

    Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 130107/65536 K (Java/Others)
    Total Submission(s): 456    Accepted Submission(s): 149


    Problem Description
    After improving the marketing strategy, TIANKENG has made a fortune and he is going to step into the status of TuHao. Nevertheless, TIANKENG wants his restaurant to go international, so he decides to name his restaurant in English. For the lack of English skills, TIANKENG turns to CC, an English expert, to help him think of a property name. CC is a algorithm lover other than English, so he gives a long string S to TIANKENG. The string S only contains eight kinds of letters-------‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’, ‘H’. TIANKENG wants his restaurant’s name to be out of ordinary, so the restaurant’s name is a string T which should satisfy the following conditions: The string T should be as short as possible, if there are more than one strings which have the same shortest length, you should choose the string which has the minimum lexicographic order. Could you help TIANKENG get the name as soon as possible?

    Meanwhile, T is different from all the substrings of S. Could you help TIANKENG get the name as soon as possible?
     
    Input
    The first line input file contains an integer T(T<=50) indicating the number of case.
    In each test case:
    Input a string S. the length of S is not large than 1000000.
     
    Output
    For each test case:
    Output the string t satisfying the condition.(T also only contains eight kinds of letters-------‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’, ‘H’.)
     
    Sample Input
    3 ABCDEFGH AAABAACADAEAFAGAH ACAC
     
    Sample Output
    AA BB B
     
    题意:给一个主串s,然后要找出一个串ans,ans是s中没出现过的子串里字典序最小的
     
    思路:很容易得知这个串的长度不会超过8,所以建一个trie树,把s中每个长度小于8的串都插入树中,最后bfs一遍看哪个结点没给标记到的就是答案。这个方法是我比赛一开始想到的好麻烦的方法。AC之后想到一个简便的方法,把他看成8进制数,abcdefg对应0到8,开一个vis数组把每个子串都标记一下,然后循环找一下就行了
     
    trie代码: 模拟进制的懒得写了
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <queue>
    using namespace std;
    const int maxnode = 2396744;
    typedef pair<int,int> pii;
    //const int maxnode = 1000000;
    const int sigma_size = 8;
    
    struct Trie{
        int ch[maxnode][sigma_size];
        int fa[maxnode];
        char let[maxnode];
        int sz;
        void init()
        {
            sz=1;
            memset(ch[0],0,sizeof(ch[0]));
        }
        int idx(char c) {return c-'A';}
        void insert(char *s,int n)
        {
            int u=0;
            for(int i=0;i<n;++i)
            {
                int c=idx(s[i]);
                if(!ch[u][c])
                {
                    memset(ch[sz],0,sizeof(ch[sz]));
                    fa[sz]=u;
                    let[sz]=c+'A';
                    ch[u][c]=sz++;
                    if(sz>=maxnode)
                        cout<<"fuck this memory"<<endl;
                }
                u=ch[u][c];
            }
        }
        char ans[10];
        int ansz;
        void bfs()
        {
            int u;
            queue<int> q;
            q.push(0);
            while(!q.empty())
            {
                u=q.front();
                q.pop();
                for(int i=0;i<sigma_size;++i)
                {
                    if(ch[u][i])
                    {
                        q.push(ch[u][i]);
                    }
                    else
                    {//find
    //                    cout<<"find "<<u<<endl;
                        ans[0]=i+'A';
                        ansz=1;
                        print(u);
                        return;
                    }
                }
            }
        }
        void print(int u)
        {
            while(u)
            {
    //            cout<<".";
                ans[ansz++]=let[u];
                u=fa[u];
            }
    //        cout<<"sz="<<ansz<<endl;
            for(int i=ansz-1;i>=0;--i)
                printf("%c",ans[i]);
            printf("
    ");
        }
        void text()
        {
            int i;
            for(i=0;i<sz;++i)
            {
                printf("%d: fa=%d let=%c
    ",i,fa[i],let[i]);
            }
        }
    };
    
    char ms[1000005];
    Trie tree;
    
    void run()
    {
        int i,len;
        scanf("%s",ms);
        len = strlen(ms);
        tree.init();
        for(i=0;i<len;++i)
        {
            tree.insert(ms+i,min(8,len-i));
        }
    //    tree.text();
        tree.bfs();
    }
    
    int main()
    {
        //freopen("in","r",stdin);
        int _;
        scanf("%d",&_);
        while(_--)
            run();
        return 0;
    }
  • 相关阅读:
    Sql例子Sp_ExecuteSql 带参数
    Flex显示麦克风当前音量
    无法将 flash.display::Sprite@156b7b1 转换为 mx.core.IUIComponent
    FMS (端口问题)如何穿透防火墙
    19:A*B问题
    6264:走出迷宫
    2753:走迷宫
    1792:迷宫
    换钱问题(经典枚举样例)
    1943(2.1)
  • 原文地址:https://www.cnblogs.com/someblue/p/4018996.html
Copyright © 2020-2023  润新知