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