Reprint please indicate the source, thank you http://blog.csdn.net/ACM_cxlove? Viewmode = Contents
By --- cxlove
Question: find two strings of LCS
Http://www.spoj.pl/problems/LCS/
Create string a as Sam first.
Run string B on Sam.
If the maximum length of the current match is L and son [B [I] exists in the current status, it indicates that the matching can be continued.
Otherwise, search along the pre.
Linear, faster than SA, but DC3 should be able to pass
# Include <iostream> # include <cstdio> # include <map> # include <cstring> # include <cmath> # include <vector> # include <algorithm> # include <set> # include <string> # include <queue> # define INF 100000005 # define M 40 # define n 510005 # define maxn 300005 # define EPS 1e-10 # define zero () FABS (a) <EPS # define min (A, B) (a) <(B )? (A) :( B) # define max (A, B) (a)> (B )? (A) :( B) # define Pb (a) push_back (a) # define MP (a, B) make_pair (a, B) # define MEM (A, B) memset (a, B, sizeof ()) # define ll unsigned long # define mod 1000000007 # define lson step <1 # define rson step <1 | 1 # define sqr (A) () * (a) # define key_value ch [CH [root] [1] [0] # define test puts ("OK"); # pragma comment (linker, "/Stack: 10241000000,1024000000") using namespace STD; struct Sam {Sam * Pre, * son [26]; int Len, G;} que [N], * root, * tail; int tot; void add (int c, int L) {Sam * P = tail, * NP = & que [tot ++]; NP-> Len = L; tail = NP; while (P & P-> son [c] = NULL) P-> son [c] = NP, P = p-> pre; if (P = NULL) NP-> pre = root; else {Sam * q = p-> son [c]; if (p-> Len + 1 = Q-> Len) NP-> pre = Q; else {Sam * NQ = & que [tot ++]; * NQ = * q; NQ-> Len = p-> Len + 1; NP-> pre = Q-> pre = NQ; while (P & P-> son [c] = q) P-> son [c] = NQ, P = p-> pre ;}}} char A [n/2], B [n/2]; int main () {scanf ("% S % s", a, B); Tot = 0; root = tail = & que [tot ++]; for (INT I = 0; A [I]; I ++) add (A [I]-'A ', I + 1); Sam * P = root; int ans = 0; For (INT I = 0, L = 0; B [I]; I ++, ans = max (ANS, L) {int c = B [I]-'A'; If (p-> son [c]) P = p-> son [c], l ++; else {While (P & P-> son [c] = NULL) P = p-> pre; if (P = NULL) P = root, L = 0; else l = p-> Len + 1, P = p-> son [c];} printf ("% d \ n", ANS); Return 0 ;}