Description
After abusing all the longest common substrings and sub-sequences, you decide to do the opposite.
a string "substring" refers to its contiguous paragraph, for example, BCD is a substring of abcdef, but BDE is not. a string of "subsequence" refers to its non-contiguous paragraph, for example, BDE is a abcdef substring, but BDD is not. below, give two lowercase string a, B, please calculate:(1) A shortest substring of a, which is not a substring of B(2) A shortest substring of a, which is not a sub-sequence of B(3) A shortest sub-sequence of a, which is not a substring of B(4) A shortest sub-sequence of a, which is not a sub-sequence of BInput
A string of two lines, one lowercase letter per line, representing A and B, respectively.
Output
Output 4 lines, one integer per line, representing the length of the answer to the above 4 questions. If no answer is met, output-1.
suffix automata and sequence automata for B
(1) (2) Ask to enumerate the left end of a substring directly, and find the first right endpoint that cannot be matched on the automaton.
(3) (4) Ask F[i][j] to consider the first I character of a string, matching the shortest sub-sequence length of node J on the automaton, the answer is F[len (A)][null]
Time Complexity O (len (A) *len (b) +len (b) *26)
#include <cstdio>#include<cstring>Const intinf=0x3f3f3f3f;Chars1[ -],s2[ -];intnx[4111][ -],l[4111],fa[4111],pv=1, ptr=1, f[4007],g[4007];intsnx[ -][ -],sp=1;voidminsint&a,intb) {if(a>b) a=b;}intMain () {scanf ("%s%s", s1+1, s2+1); intL1=strlen (s1+1); intL2=strlen (s2+1); for(intI=1; i<=l1;++i) s1[i]-='a'; for(intI=1; i<=l2;++i) s2[i]-='a'; for(intI=1; i<=l2;++i) { intx=S2[i]; for(intJ=SP++;J&&!SNX[J][X];--J) snx[j][x]=sp; intp=pv,np=++ptr;pv=NP; L[NP]=l[p]+1; while(P&&!nx[p][x]) nx[p][x]=np,p=Fa[p]; if(!p) fa[np]=1; Else{ intq=Nx[p][x]; if(l[q]==l[p]+1) fa[np]=Q; Else{ intnq=++ptr; memcpy (Nx[nq],nx[q], -); L[NQ]=l[p]+1; FA[NQ]=Fa[q]; FA[Q]=fa[np]=NQ; while(p&&nx[p][x]==q) nx[p][x]=nq,p=Fa[p]; } } } intans=inf; for(intI=1; i<=l1;++i) { for(intj=i,w=1; j<=l1;++j) {W=Nx[w][s1[j]]; if(!W) {mins (ans,j-i+1); Break; }}} printf ("%d\n", ans==inf?-1: ans); Ans=inf; for(intI=1; i<=l1;++i) { for(intj=i,w=1; j<=l1;++j) {W=Snx[w][s1[j]]; if(!W) {mins (ans,j-i+1); Break; }}} printf ("%d\n", ans==inf?-1: ans); Memset (F,0x3f,sizeof(int) * (ptr+1)); Memset (g,0x3f,sizeof(int) * (ptr+1)); f[1]=g[1]=0; for(intI=1; i<=l1;++i) { intx=S1[i]; for(intj=1; j<=ptr;++j) {mins (G[nx[j][x]],f[j]+1); } memcpy (F,g,sizeof(int) * (ptr+1)); } printf ("%d\n", f[0]==inf?-1: f[0]); Memset (F,0x3f,sizeof(int) * (sp+1)); Memset (g,0x3f,sizeof(int) * (sp+1)); f[1]=g[1]=0; for(intI=1; i<=l1;++i) { intx=S1[i]; for(intj=1; j<=sp;++j) {mins (G[snx[j][x]],f[j]+1); } memcpy (F,g,sizeof(int) * (sp+1)); } printf ("%d\n", f[0]==inf?-1: f[0]); return 0;}
bzoj4032: [HEOI2015] shortest not common substring