Shortest letter string

Ac_automaton pressure in combination with shape.

Look question is dp explanation on Ac_automaton, but in fact there is no obvious transfer process, using only the pressure of record-like manner whether a string is selected after (of course there are built complete solution Ac_automation then run pure form of pressure dp).

Firstly Ac_automaton (Trie FIG), to maintain a sta additional array, the array is represented by a binary array, '1' indicates that the string occurred, '0' indicates that the string is appeared. This information needs to insert statistics and generate (build) process, however, interesting to note that most of the issues handled by the only online solution fail to update the current node, and the information is not transferred over from his father, but still can A swap.

As for the principle of maintenance, due to the longest suffix pointer to fail, imagine a string suffix have emerged, he could not emerge? Then think of the father of the string of a string (a string that contains it, and substring relative) have emerged, and that it may not appear right.

How then is to solve ans. First think, and how we can meet the minimum lexicographical problems.

bfs, since the characteristics of the outer layers of push, so that when we traversed a viable state, then he is the shortest, as a "layer" we say at this time is the length.

Usually long lexicographic from A-> Z enumeration can be guaranteed (this ratio should be a miss).

The remaining problem is to record the path, we can maintain an id in bfs structure, the push represents the current state to the next state, the next state father state number (it seems that trouble), we use the following method before the after the record to the state by the state from, to find the target state, direct output can be recursive.

In fact, quite bad thinking too.

Specific implementation details can refer to the code pointer kick after a little time out (I later discovered that somehow there is a map of logn), into an array of records to facilitate that.

 

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<set>
#include<map>
using namespace std;
struct node{  
    int p,state,id;
};
struct Pre{  
    int nex,num;
}pre[60000000];
int trie[6000][26];
int n,tot,root,cnt,lis[6000];
int sta[6000],fail[6000];
char s[60];
bool v[6000][1<<15];
void mclear(){  
    tot=root=1;    
}
void insert(int x){  
    int now=root,l=strlen(s+1);
    for(int i=1;i<=l;i++){  
        if(!trie[now][s[i]-'A']) trie[now][s[i]-'A']=++tot;
        now=trie[now][s[i]-'A'];    
    }sta[now]|=(1<<(x-1));
}
void generate(){  
    queue<int>q;
    for(int i=0;i<26;i++)
        if(trie[root][i]){  
            fail[trie[root][i]]=root;
            q.push(trie[root][i]);
        }else trie[root][i]=root;
    while(!q.empty()){  
        int now=q.front();
        q.pop();
        for(int i=0;i<26;i++)
                if(trie[now][i]){  
                    fail[trie[now][i]]=trie[fail[now]][i];    
                    q.push(trie[now][i]);
                    sta[trie[now][i]]|=sta[now];
                }else trie[now][i]=trie[fail[now]][i];
    }
    for(int i=1;i<=tot;i++)
        sta[i]|=sta[fail[i]];
}
void print(int x){  
    if(!x) return ;
    print(pre[x].nex);
    printf("%c",pre[x].num+'A');
}
void bfs(){  
    queue<node>q;int head=0,tail=1;
    q.push((node){1,0});
    v[1][0]=1;
    int ends=(1<<n)-1;
    while(!q.empty()){  
        node x=q.front();
        q.pop();
        int now=x.p,stt=x.state,id=x.id;
        if(ends==stt){  
                print(id);
                return ;    
        }
        for(int i=0;i<26;i++){  
            int y=trie[now][i];
            if(v[y][sta[y]|stt]) continue;
            pre[++cnt].nex=id;
            pre[cnt].num=i;
            v[y][sta[y]|stt]=1;
            q.push((node){y,sta[y]|stt,cnt});
        }    
    }
}
int main(){  
    scanf("%d",&n);
    mclear();
    for(int i=1;i<=n;i++){  
        scanf("%s",s+1);
        insert(i);
    }
    generate();
    bfs();
    return 0;
}
View Code

 

 

 

Pay a small oj, the results of this day bzoj like maintenance, did not test the code, indented a bit ugly, make do see it, the main pressure-like and understand why bfs like, understand the Internet to find some solution to a problem like the pressure dp ( Although this problem is mainly to learn the Ac_automaton).

Guess you like

Origin www.cnblogs.com/Yu-shi/p/11072109.html