Title Link: http://poj.org/problem?id=2774
Test instructions: The longest common substring length of a string, given two lowercase characters.
Idea: Depending on the << suffix array-a powerful tool for handling strings >>, any substring of a string is prefixed with a suffix of the string. PleaseAand theBis equivalent to the longest common substring in theAthe suffix andBThe maximum value of the longest public prefix of the suffix. If the enumerationAand theBall of the suffixes,then it is obviously inefficient. Because you want to calculateAthe suffix andBThe longest common prefix of the suffix, so first write the second string after the first string, the question is separated by a character that does not appear., then ask for the suffix array for this new string.
so isn't all theHeightthe maximum value in the value is the answer.?Not necessarily!It is possible that the two suffixes are in the same string., so actually only whensuffix (sa[i-1])and thesuffix (sa[i])Is not a two suffix in the same string, Height[i]is full of conditions. And the biggest of these is the answer. Remember stringAand StringBthe length of each islAland thelBl. An array of suffixes for the novelty string andHeightthe time of the array is0 (ial+l Bl),and then find the two suffixes that are next to each other but are not in the same string.Heightthe maximum value of the value, Time is0 (LAL+LBL),so the time complexity of the whole procedure is0 (LAL+LBL).
#define_crt_secure_no_deprecate#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<queue>#include<vector>#include<time.h>#include<cmath>using namespaceStd;typedefLong Long intLL;Const intMAXN =200000+5;intcmpint*r,intAintBintl) { returnR[a] = = R[b] && r[a + l] = = R[b +l];}intWA[MAXN], WB[MAXN], WV[MAXN], WS[MAXN];voidDaint*r,int*sa,intNintm) { intI, J, p, *x = WA, *y = WB, *T; for(i =0; I < m; i++) {Ws[i] =0; } for(i =0; I < n; i++) {Ws[x[i] = r[i]]++; } for(i =1; I < m; i++) {Ws[i] + = ws[i-1]; } for(i = n-1; I >=0; i--) {Sa[--ws[x[i]] =i;} for(j =1, p =1; p<n; J *=2, M =p) { for*pb=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++) {Ws[i] =0; } for(i =0; I < n; i++) {ws[wv[i]]++; } for(i =1; I < m; i++) {Ws[i] + = ws[i-1]; } for(i = n-1; I >=0; i--) {Sa[--ws[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;}intRANK[MAXN], HEIGHT[MAXN], SA[MAXN];voidCalheight (int*r,int*sa,intN) { intI, 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;}intR[MAXN], Ca =1, Len,index;CharSTR[MAXN],CH[MAXN];voidsolve () {intAns =0; for(inti =1; i < Len; i++){ intL = Height[i];//Public prefixes intFidx = min (Sa[i-1], sa[i]); intSIDX = max (Sa[i-1], sa[i]); if(Fidx<index&&sidx>index) {//The starting point of the two suffixes is distributed around ' # ' on both endsAns =Max (ans, L); }} printf ("%d\n", ans);}intMain () {//#ifdef Kirito//freopen ("In.txt", "R", stdin);//freopen ("OUT.txt", "w", stdout);//#endif//int start = Clock (); while(~SCANF ("%s%s", Str,ch)) {Index=strlen (str); strcat (str,"#"); strcat (str, CH); Len=strlen (str); for(inti =0; i < Len; i++){ if(Str[i] = ='#') {R[i] =0;Continue; } R[i]= str[i]-'a'+1; } da (R, SA, Len, -); Calheight (R, SA, Len-1); Solve (); }//#ifdef Local_time//cout << "[Finished in" << clock ()-Start << "MS" << Endl;//#endif return 0;}
POJ 2774 suffix Array