Spoj 1811 Longest Common substring (SAM)

Source: Internet
Author: User

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 ;}

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.