http://www.spoj.com/problems/LCS2/
Found a pit of my original understanding of Sam 233
It is easy to see that all matching lengths are recorded in the state and then take Min before taking Max for all states.
But don't forget one thing: Update the ancestors of the parent tree.
Why is it? First if the subtree is matched, the length must be greater than the length of any ancestor match (even some ancestor match length is 0!). Why, because we in the process of matching, just find a substring, may also omit the ancestor did not match to, so that the ancestor's record value is 0, then in the corresponding state to the min will take 0, so that WA. Also note that if the current node is matched, the ancestors must be able to assign values to the ancestors ' length! Because the length of the current node is greater than any ancestor. (
such as data
Acbbc
Bc
Ac
The answer should be 1, right. If the ancestors were not updated, the answer would be 0.
Just think it over.
So remember later: when it comes to any number of strings, it is important to note whether the subtree of the current state is better than the current state record when the same state is evaluated.
#include <cstdio> #include <cstring> #include <cmath> #include <string> #include <iostream > #include <algorithm> #include <queue> #include <set> #include <map>using namespace std; typedef long Long LL; #define REP (i, n) for (int i=0; i< (n); ++i) #define FOR1 (i,a,n) for (int i= (a); i<= (n); ++i) #define For2 (i,a,n) for (int i= (a);i< (n), ++i) #define FOR3 (i,a,n) for (int i= (a); i>= (n); i.) #define FOR4 (i,a,n) for (int i= ( a);i> (n); i) #define CC (i,a) memset (i,a,sizeof (i)) #define READ (a) a=getint () #define PRINT (a) printf ("%d", a) # Define DBG (x) cout << (#x) << "=" << (x) << endl#define error (x) (! x) puts ("error"): 0) #define RDM (x, i) for (int i=ihead[x]; i; i=e[i].next) inline const int Getint () {int r=0, k=1; Char c=g Etchar (); for (; c< ' 0 ' | | C> ' 9 '; C=getchar ()) if (c== '-') k=-1; for (; c>= ' 0 ' &&c<= ' 9 '; C=getchar ()) r=r*10+c-' 0 '; return k*r; }struct sam {static const int n=250005;int c[n][26], l[n], F[n], ROot, last, CNT, Mx[n], X[n];sam () {cnt=0; root=last=++cnt;} void Add (int x) {int now=last, a=++cnt; last=a;l[a]=l[now]+1;for (; now &&!c[now][x]; Now=f[now]) c[now][x]=a;if ( !now) f[a]=root;else {int q=c[now][x];if (l[q]==l[now]+1) f[a]=q;else {int b=++cnt;memcpy (c[b], c[q], sizeof c[q]); l[b]= L[now]+1;f[b]=f[q];f[q]=f[a]=b;for (; now && c[now][x]==q; Now=f[now]) c[now][x]=b;}} void Build (char *s) {int Len=strlen (s); Rep (I, Len) Add (s[i]-' a '); For1 (i, 1, CNT) Mx[l[i]]++;for1 (i, 1, len) mx[i]+=mx[i-1] ; For1 (i, 1, CNT) X[mx[l[i]]--]=i;for1 (i, 1, CNT) mx[i]=l[i];} void Find (char *s) {int now=root, t=0, Len=strlen (s), static int Arr[n];rep (I, len) {int k=s[i]-' a '; if (C[now][k]) ++t, now= C[now][k];else {while (now &&!c[now][k]) now=f[now];if (!now) t=0, Now=root;else t=l[now]+1, now=c[now][k];} Arr[now]=max (Arr[now], t);} For3 (i, CNT, 1) {t=x[i];mx[t]=min (mx[t], arr[t]), if (Arr[t] && f[t]) arr[f[t]]=l[f[t]];arr[t]=0;}} int Getans () {int Ret=0;for1 (i, 1, CNT) Ret=max (ret, MX[i]); return ret;}} A;const int N=100005;char S[n];int main () {scanf ("%s", s), A.build (s), while (~SCANF ("%s", s)) A.find (s);p rint (A.getans () ); return 0;}
A string is finite sequence of characters over a non-empty finite setσ.
In this problem,σis the set of lowercase letters.
Substring, also called Factor, is a consecutive sequence of characters occurrences at least once in a string.
Now your task was a bit harder, for some given strings, find the length of the longest common substring of them.
Here common substring means a substring of the or more strings.
Input
The input contains at most lines, each line consists of no more than 100000 lowercase letters, representing a string.
Output
The length of the longest common substring. If such string doesn ' t exist, print "0" instead.
Example
Input:alsdfkjfjkdsalfdjskalajfkdslaaaaajfaaaaoutput:2
Notice:new Testcases Added
"Spoj" 1812. Longest Common Substring II (suffix automaton)