Ural1517 freedom of choice, suffix array, Longest Common substring

Source: Internet
Author: User

Also look at the set of questions in the suffix array...


/*************************************** **************************************** # Author: neo Fung # Email: neosfung@gmail.com # Last modified: # filename: ural1517 freedom of choice. CPP # Description: Because my multiplication algorithm requires that except the last bit of the string, the other bit cannot be 0, otherwise ************************************* **************************************** */# ifdef _ msc_ver // # define debug // # DEFINE _ crt_secure_no_depre Cate # endif # include <fstream> # include <stdio. h> # include <iostream> # include <string. h> # include <string> # include <limits. h> # include <algorithm> # include <math. h> # include <numeric> # include <functional> # include <ctype. h> using namespace STD; const int kmaxn = 210010; int W [kmaxn], wa [kmaxn], WB [kmaxn], WV [kmaxn], array [kmaxn], sa [kmaxn]; // int CMP (const int * r, const Int & A, const Int & B, const Int & L) {return R [a] = = R [B] & R [A + L] = R [B + L];} // multiply algorithm/r is the array to be compared; n is the length of R; m is the maximum value of the element in R void doublingalgorithm (const int * r, int * Sa, const Int & N, int m) {int I, j, p, * x = wa, * Y = WB, * t; for (I = 0; I <m; I ++) W [I] = 0; for (I = 0; I <n; I ++) W [x [I] = R [I] ++; for (I = 1; I <m; I ++) W [I] + = W [I-1]; for (I = n-1; I> = 0; I --) sa [-- W [x [I] = I; for (P = 1, j = 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 <M; I ++) W [I] = 0; for (I = 0; I <n; I ++) W [wv [I] = x [Y [I] ++; for (I = 1; I <m; I ++) W [I] + = W [I-1]; for (I = n-1; I> = 0; I --) sa [-- W [wv [I] = Y [I]; for (t = x, x = Y, y = T, P = 1, I = 1, X [SA [0] = 0; I <n; I ++) x [SA [I] = CMP (Y, sa [I-1], sa [I], j )? P-1: P ++;} return;} // calculate the height array. The definitions of R and SA are the same as those of; // The subscript of height starts from 1 and ends with N, including N; int g_rank [kmaxn], height [kmaxn]; void calculateheight (const int * r, int * Sa, const Int & N) {int I, j, k = 0; for (I = 1; I <= N; I ++) g_rank [SA [I] = I; for (I = 0; I <N; height [g_rank [I ++] = k) for (K? K --: 0, j = sa [g_rank [I]-1]; R [I + k] = R [J + k]; k ++); return ;} int main (void) {# ifdef debug freopen (".. /stdin.txt "," r ", stdin); freopen (".. /stdout.txt "," W ", stdout); # endif int N, ncase = 1; char STR [kmaxn]; // scanf (" % d ", & ncase ); while (~ Scanf ("% d", & N) {getchar (); gets (STR); For (INT I = 0; I <n; ++ I) array [I] = STR [I]; array [N] = 1; gets (STR); For (INT I = 0; I <n; ++ I) array [I + n + 1] = STR [I]; array [n + 2] = 0; doublingalgorithm (array, SA, N + 2,256 ); calculateheight (array, SA, N + n + 1); int max_height = 1, idx = 0; For (INT I = 1; I <n + 2; + + I) {If (max_height 

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.