zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    精英讲师培训笔记12-即兴演讲如何与众不同
    精英讲师培训笔记11-上台演讲前
    精英讲师培训笔记10-上台演讲三技巧
    精英讲师培训笔记09-如何化解提问无人回答
    精英讲师培训笔记08-如何快速吸引学员注意力
    精英讲师培训笔记07-如何设计你的演讲,让更具吸引力
    精英讲师培训笔记06-学员不回答问题,课堂死气沉沉怎么办
    精英讲师培训笔记05-8个字搞定即兴演讲
    精英讲师培训笔记04-学员在玩手机、聊天、睡觉、走神怎么办?
    编程生涯——追逐朝霞的日子
  • 原文地址:https://www.cnblogs.com/someblue/p/4018996.html
Copyright © 2011-2022 走看看