Spoj1811 Longest Common Substring, suffix Automatic Machine

Source: Internet
Author: User

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
    
     

Related Article

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.