Hdu4886 tiankeng's restaurant (ⅱ) (trie tree or simulated hexadecimal)

Source: Internet
Author: User

Tiankeng's restaurant (II)

Time Limit: 16000/8000 MS (Java/others) memory limit: 130107/65536 K (Java/Others)
Total submission (s): 456 accepted submission (s): 149


Problem descriptionafter 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 Engl Ish, 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 shocould satisfy the following conditions: the string t shocould be as short as possible, if there are more than one strings which have the same shortest Len Please, you shocould choose the string which has the minimum Lexicographic Order. cocould you help tiankeng get the name as soon as possible?

Meanwhile, t is different from all the substrings of S. cocould you help tiankeng get the name as soon as possible?

 

Inputthe first line input file contains an integer T (t <= 50) indicating the number of cases.
In each test case:
Input a string S. The length of S is not large than 1000000.

 

Outputfor 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 input3abcdefghaaabaacadaeafagahacac

 

Sample outputaabbb: Give a main string s, and then find a string ans. ans is the smallest idea of the Lexicographic Order in the substrings that haven't appeared in S: it is easy to know that the length of this string will not exceed 8. Therefore, create a trie tree and insert each string whose length is less than 8 in s into the tree, finally, BFs checks which node is not marked as the answer. This method is a very troublesome method that I thought of at the beginning of the competition. The AC then comes up with a simple method, which is regarded as an octal number. abcdefg corresponds to 0 to 8. Open a vis array and mark each substring, the trie code can be found cyclically: It's too easy to write the code to simulate the hexadecimal format.
#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("\n");    }    void text()    {        int i;        for(i=0;i<sz;++i)        {            printf("%d: fa=%d let=%c\n",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;}

 

Hdu4886 tiankeng's restaurant (ⅱ) (trie tree or simulated hexadecimal)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.