[Search for the longest public substring In the suffix array] poj 2774

Source: Internet
Author: User
Tags first string

According to Luo's thesis, an ascii code must be added to the middle of the two strings, which is a character smaller than any letter.

Longest public substring (pku2774, ural1517)
Returns the longest common substring of two strings A and B.
Algorithm analysis:
Any substring of a string is the prefix of A suffix of the string. The longest common substring of A and B is equivalent to the maximum value of the longest common prefix of A and B. If you enumerate all the suffixes of A and B, this is obviously inefficient. Because the suffix of A and the longest common prefix of B are to be calculated, the second string is first written after the first string, separated by a character that has not appeared in the middle, then find the suffix array of the new string. Check to see if you can find some rules from the suffix array of the new string. Take a = "aaaba" and B = "abaa" as an example.

Is the maximum value in all height values the answer? Not necessarily! It is possible that these two suffixes are in the same string, so in fact only when suffix (SA [I-1]) and suffix (SA [I]) are not two Suffixes in the same string, height [I] meets the conditions. The maximum value is the answer. The lengths of string a and string B are respectively | A | and | B |. The time required to obtain the suffix array and height array of the new string is O (| A | + | B | ), then calculate the maximum value of the height value of the two suffixes that are adjacent but not in the same string. The time is also O (| A | + | B | ), therefore, the time complexity of the entire practice is O (| A | + | B | ). The time complexity has reached the lower limit. It can be seen that this is a very good algorithm.

# Define maxn 2111int wa [maxn], WB [maxn], WV [maxn], and WSS [maxn]; // The ws of the thesis template conflicts with the keywords in G ++! Int R [maxn], sa [maxn]; int CMP (int * r, int A, int B, int L) {return R [a] = R [B] & R [A + L] = R [B + L];}/* [multiplier algorithm O (nlgn)] Put the string to be sorted in the r array, from R [0] To R [n-1], with a length of N and the maximum value less than m before the multiplication algorithm is used, make sure that the values of the r array are greater than 0. Then add a 0 character to the original string. If the length of the original string is N, the length of the r array constructed by the suffix array should be n + 1. therefore, when calling the DA function, the corresponding N should be n + 1. */void da (int * r, int * Sa, int N, int m) {// n should be added with 1 int I, j, P, * x = wa, * Y = WB, * t; for (I = 0; I <m; I ++) WSS [I] = 0; for (I = 0; I <N; I ++) WSS [x [I] = R [I] ++; for (I = 1; I <m; I ++) WSS [I] + = WSS [I-1]; for (I = n-1; I> = 0; I --) SA [-- WSS [x [I] = I; for (j = 1, P = 1; P <n; j * = 2, M = P) {for (P = 0, I = N-J; I <N; I ++) y [p ++] = I; for (I = 0; I <n; I ++) if (SA [I]> = J) Y [p ++] = sa [I]-J; for (I = 0; I <N; I ++) WV [I] = x [Y [I]; for (I = 0; I <m; I ++) WSS [I] = 0; for (I = 0; I <n; I ++) WSS [wv [I] ++; for (I = 1; I <m; I ++) WSS [I] + = WSS [I-1]; for (I = n-1; I> = 0; I --) sa [-- WSS [wv [I] = Y [I]; for (t = x, x = Y, y = T, P = 1, X [SA [0] = 0, I = 1; I <n; I ++) x [SA [I] = CMP (Y, sa [I-1], sa [I], j )? P-1: P ++;} return;} int rank [maxn], height [maxn]; // rank [I]: the number of the I-th row; SA [I]: where is the suffix string of the column I? inverse void calheight (int * r, int * Sa, int N) for each other {// n no 1 int I, j, k = 0; for (I = 1; I <= N; I ++) rank [SA [I] = I; for (I = 0; I <N; height [rank [I ++] = k) {for (K? K --: 0, j = sa [rank [I]-1]; R [I + k] = R [J + k]; k ++ );} return;} int rmq [maxn]; int mm [maxn]; int best [20] [maxn]; void initrmq (int n) {int I, j, A, B; for (Mm [0] =-1, I = 1; I <= N; I ++) mm [I] = (I & (I-1 )) = 0 )? Mm [I-1] + 1: Mm [I-1]; for (I = 1; I <= N; I ++) Best [0] [I] = I; for (I = 1; I <= mm [N]; I ++) for (j = 1; j <= n + 1-(1 <I ); j ++) {A = Best [I-1] [J]; B = Best [I-1] [J + (1 <(I-1)]; if (rmq [a] <rmq [B]) Best [I] [J] = A; else best [I] [J] = B;} return ;} int askrmq (int A, int B) {int t; t = mm [B-A + 1]; B-= (1 <t)-1; A = Best [T] [a]; B = Best [T] [B]; return rmq [a] <rmq [B]? A: B;} int LCP (int A, int B) {// The Longest Common preorder int t; A = rank [a]; B = rank [B]; if (A> B) {T = A; A = B; B = T;} return (height [askrmq (a + 1, B)]);} char A [maxn], B [maxn]; int main () {While (scanf ("% S % s", a, B )! =-1) {int n, m; int I; int Len = strlen (a); strcat (a, "#"); strcat (A, B ); N = strlen (a); M = 0; for (I = 0; I <n; I ++) {M = max (M, (INT) A [I]); R [I] = A [I];} da (R, SA, n + 1, m + 1 ); // note that n + 1 int res = 0; calheight (R, SA, n); // note that N does not need to be added with 1 for (I = 1; I <N; I ++) {If (SA [I-1] <= Len & SA [I]> Len) | (SA [I-1]> Len & SA [I] <= Len) {// both suffixes must belong to two strings res = max (Res, height [I]) ;}} printf ("% d \ n", Res) ;}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.