Bzoj 4032 HEOI2015 Shortest non-common substring suffix automaton + sequence automaton +bfs

Source: Internet
Author: User

Topic: Given Strings A and b, the shortest substring/sub-sequence s satisfies S is not a substring/sub-sequence of b
That's a really toxic problem.
Pointers is similar to this problem
Then the substring is the suffix automatic machine sequence is naturally the sequence automaton = =
Every time an X node is updated, any subsequent nodes of X are connected to this node
The parent of each node is where the letter last appeared
Each letter records the last occurrence of the position when the pointer is updated along the parent pointer.

#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[ -],*parent;intMAX_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-&GT;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[ -],*parent; }mempool[m],*c; Sqa*root,*last[ -]; Sequence_automaton () {c=mempool; Last[ -]=root=new (c + +) Sqa; } void Extend (int x) {Sqa*p,*NP=new (c + +) Sqa;intI for(i=0; i<= -; 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];intV[M][M];intBFS1 ()//str-str{intI,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< -; i++) {if(!substr_a.mempool[sta.x].son[i])Continue;if(!substr_b.mempool[sta.y].son[i])returnsta.dpt+1;intXx=substr_a.mempool[sta.x].son[i]-substr_a.root;intYy=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;}intBFS2 ()//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;}intBFS3 ()//seq-str{intI,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< -; i++) {if(!subseq_a.mempool[sta.x].son[i])Continue;if(!substr_b.mempool[sta.y].son[i])returnsta.dpt+1;intXx=subseq_a.mempool[sta.x].son[i]-subseq_a.root;intYy=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;}intBFS4 ()//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;}intMain () {//freopen("Sus.in","R", stdin);//freopen("Sus.out","W", stdout);intI 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&LT;&LT;BFS1 () <<endl;    COUT&LT;&LT;BFS2 () <<endl;    COUT&LT;&LT;BFS3 () <<endl; COUT&LT;&LT;BFS4 () <<endl;return 0;}

Bzoj 4032 HEOI2015 Shortest non-common substring suffix automaton + sequence automaton +bfs

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.