Spoj1811 Longest Common Substring, suffix Automatic Machine
Spoj1811LCS
The longest common substring of two strings.
The procedure is simple. If the match is successful, tl ++ fails to roll back from the parent pointer. tl = t [now]. len.
From this question, we can clearly understand the nature of the fa pointer of the suffix automatic machine:
Point to a status. The acceptance string s [x .. x + I] is an accepted string suffix with the current status s [j-I .. j] matching is the longest one.
Is there a similar nature as KMP?
KMP uses the next array to roll back the data in case of mismatch. the position I to which the data is backed is s [0 .. i] and the current string suffix s [j-I .. j] The longest match.
So.
Suffix automatic machines can be used to obtain the longest matching length between a substring (s [x ..]) and another substring.
KMP can solve the longest matching length between a string (s [0 ..]) and a substring of another string.
#include
#include
#include
#includeusing namespace std;#define Maxn 250100int root,last;//samint tots;struct sam_node{ int fa,son[26]; int len; void init(int _len){len=_len;fa=-1;memset(son,-1,sizeof(son));}}t[Maxn*2];//length*2void sam_init(){ tots=0; root=last=0; t[tots].init(0);}void extend(char ch){ int w=ch-'a'; int p=last; int np=++tots;t[tots].init(t[p].len+1); int q,nq; while(p!=-1&&t[p].son[w]==-1){t[p].son[w]=np;p=t[p].fa;} if (p==-1) t[np].fa=root; else{ q=t[p].son[w]; if (t[p].len+1==t[q].len){t[np].fa=q;} else{ nq=++tots;t[nq].init(0); t[nq]=t[q]; t[nq].len=t[p].len+1; t[q].fa=nq;t[np].fa=nq; while(p!=-1&&t[p].son[w]==q){t[p].son[w]=nq;p=t[p].fa;} } } last=np;}char s[Maxn];char f[Maxn];int work(int l2){ int i,now=root,ind,tl=0; int ret=0; for(i=0;i