BZOJ 4032 HEOI2015 shortest non-public sub-string suffix automation + sequence automation + BFS, bzojheoi2015

Source: Internet
Author: User

BZOJ 4032 HEOI2015 shortest non-public sub-string suffix automation + sequence automation + BFS, bzojheoi2015

Given strings A and B, find the shortest substring/subsequence S to satisfy the substring/subsequence S which is not B
This question is really TM toxic * 2
Similar to this question
Then the sub-string is a suffix. The sub-sequence of the automatic machine is naturally a sequential automatic machine =.
Each time an x node is updated, all subsequent nodes without x are connected to this node.
The parent of each node is the last occurrence of the letter.
Each letter records the last position that appears when updating the pointer along the parent pointer again.

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define M 4040using namespace std;char A[M],B[M];struct Suffix_Automaton{    struct Sam{        Sam *son[26],*parent;        int max_dpt;        Sam() {}         Sam(int _):max_dpt(_) {}    }mempool[M],*C;    Sam *root,*last;    Suffix_Automaton()    {        C=mempool;        last=root=new (C++)Sam(0);    }    void Extend(int x)    {        Sam *p=last;        Sam *np=new (C++)Sam(p->max_dpt+1);        for(;p&&!p->son[x];p=p->parent)            p->son[x]=np;        if(!p)            np->parent=root;        else        {            Sam *q=p->son[x];            if(p->max_dpt+1==q->max_dpt)                np->parent=q;            else            {                Sam *nq=new (C++)Sam(p->max_dpt+1);                memcpy(nq->son,q->son,sizeof nq->son);                nq->parent=q->parent;                q->parent=nq;np->parent=nq;                for(;p&&p->son[x]==q;p=p->parent)                    p->son[x]=nq;            }        }        last=np;    }}substr_A,substr_B;struct Sequence_Automaton{    struct Sqa{        Sqa *son[26],*parent;    }mempool[M],*C;    Sqa *root,*last[27];    Sequence_Automaton()    {        C=mempool;        last[26]=root=new (C++)Sqa;    }    void Extend(int x)    {        Sqa *p,*np=new (C++)Sqa;        int i;        for(i=0;i<=26;i++)            for(p=last[i];p&&!p->son[x];p=p->parent)                p->son[x]=np;        np->parent=last[x];        last[x]=np;    }}subseq_A,subseq_B;struct abcd{    int x,y,dpt;    abcd() {}    abcd(int _,int __,int ___):        x(_),y(__),dpt(___) {}}q[M];int v[M][M];int BFS1()//str-str{    int i,r=0,h=0;    q[++r]=abcd(0,0,0);    v[0][0]=true;    while(r!=h)    {        abcd sta=q[++h];        for(i=0;i<26;i++)        {            if(!substr_A.mempool[sta.x].son[i]) continue;            if(!substr_B.mempool[sta.y].son[i]) return sta.dpt+1;            int xx=substr_A.mempool[sta.x].son[i]-substr_A.root;            int yy=substr_B.mempool[sta.y].son[i]-substr_B.root;            if(v[xx][yy]==1) continue;            v[xx][yy]=1;            q[++r]=abcd(xx,yy,sta.dpt+1);        }    }    return -1;}int BFS2()//str-seq{    int i,r=0,h=0;    q[++r]=abcd(0,0,0);    v[0][0]=true;    while(r!=h)    {        abcd sta=q[++h];        for(i=0;i<26;i++)        {            if(!substr_A.mempool[sta.x].son[i]) continue;            if(!subseq_B.mempool[sta.y].son[i]) return sta.dpt+1;            int xx=substr_A.mempool[sta.x].son[i]-substr_A.root;            int yy=subseq_B.mempool[sta.y].son[i]-subseq_B.root;            if(v[xx][yy]==2) continue;            v[xx][yy]=2;            q[++r]=abcd(xx,yy,sta.dpt+1);        }    }    return -1;}int BFS3()//seq-str{    int i,r=0,h=0;    q[++r]=abcd(0,0,0);    v[0][0]=true;    while(r!=h)    {        abcd sta=q[++h];        for(i=0;i<26;i++)        {            if(!subseq_A.mempool[sta.x].son[i]) continue;            if(!substr_B.mempool[sta.y].son[i]) return sta.dpt+1;            int xx=subseq_A.mempool[sta.x].son[i]-subseq_A.root;            int yy=substr_B.mempool[sta.y].son[i]-substr_B.root;            if(v[xx][yy]==3) continue;            v[xx][yy]=3;            q[++r]=abcd(xx,yy,sta.dpt+1);        }    }    return -1;}int BFS4()//seq-seq{    int i,r=0,h=0;    q[++r]=abcd(0,0,0);    v[0][0]=true;    while(r!=h)    {        abcd sta=q[++h];        for(i=0;i<26;i++)        {            if(!subseq_A.mempool[sta.x].son[i]) continue;            if(!subseq_B.mempool[sta.y].son[i]) return sta.dpt+1;            int xx=subseq_A.mempool[sta.x].son[i]-subseq_A.root;            int yy=subseq_B.mempool[sta.y].son[i]-subseq_B.root;            if(v[xx][yy]==4) continue;            v[xx][yy]=4;            q[++r]=abcd(xx,yy,sta.dpt+1);        }    }    return -1;}int main(){    //freopen("sus.in","r",stdin);    //freopen("sus.out","w",stdout);    int i;    scanf("%s%s",A+1,B+1);    for(i=1;A[i];i++)    {        substr_A.Extend(A[i]-'a');        subseq_A.Extend(A[i]-'a');    }    for(i=1;B[i];i++)    {        substr_B.Extend(B[i]-'a');        subseq_B.Extend(B[i]-'a');    }    cout<<BFS1()<<endl;    cout<<BFS2()<<endl;    cout<<BFS3()<<endl;    cout<<BFS4()<<endl;    return 0;}

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.