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->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<<BFS1 () <<endl; COUT<<BFS2 () <<endl; COUT<<BFS3 () <<endl; COUT<<BFS4 () <<endl;return 0;}
Bzoj 4032 HEOI2015 Shortest non-common substring suffix automaton + sequence automaton +bfs