Spoj LCS suffix automaton to find the largest common substring

Source: Internet
Author: User

Here, after the completion of the suffix automaton with the first string

Continuously use the second string from left to right along the suffix automaton to go forward, if found, then the current matching number plus 1

If you can't find it, continue along the suffix tree to find the maximum length you can match to the current character, then move the CUR node to the current node and add 1 to the answer.

Remember to keep updating the maximum value you can get

1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5 using namespacestd;6 #defineM 267 #defineN 6000008 intCNT;9 CharS1[n], s2[n];Ten  One structsamnode{ ASamnode *son[m], *F; -     intl; -}sam[n], *root, *Last ; the  - voidInit () - { -Memset (Sam,0,sizeof(SAM)); +root = last = &sam[cnt=0]; - } +  A voidAddintx) at { -Samnode *p = &sam[++cnt], *JP =Last ; -P->l = jp->l+1; -Last =p; -      for(; jp&&!jp->son[x]; jp=jp->f) jp->son[x] =p; -     if(!JP) P->f =Root; in     Else{ -         if(jp->l+1= = jp->son[x]->l) P->f = jp->Son[x]; to         Else{ +Samnode *r = &sam[++cnt], *q = jp->Son[x]; -*r = *Q; theR->l = jp->l+1; Q->f = P->f =R; *              for(; JP && jp->son[x]==q; jp=jp->f) jp->son[x]=R; $         }Panax Notoginseng     } - } the  + intSolveintlen) A { the     intRET =0, maxn=0; +Samnode *cur =Root; -      for(intI=0; I<len; i++){ $         intx = s2[i]-'a'; $         if(cur->Son[x]) { -ret++; -Cur = cur->Son[x]; the         } -         Else{Wuyi              while(cur &&!cur->son[x]) cur = cur->F; the             if(!cur) cur = root, ret=0; -             Else{ WuRET = cur->l+1; -Cur = cur->Son[x]; About             } $         } -MAXN =Max (MAXN, ret); -     } -     returnMAXN; A } +  the intMain () - { $    //freopen ("In.txt", "R", stdin); the    //freopen ("OUT.txt", "w", stdout); thescanf"%s%s", S1, S2); the init (); the     intLen1 = strlen (S1), len2 =strlen (S2); -      for(intI=0; I<len1; i++) Add (s1[i]-'a'); in     intRET =solve (len2); theprintf"%d\n", ret); the     return 0; About}

Spoj LCS suffix automaton to find the largest common substring

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.