The S1,S2 stitching, to find height. The adjacent height determines whether the starting point of the left and right string is in two strings, and the height and s1.length ()-sa[i-1] Take min.
#include <iostream>#include<cstring>#include<string>#include<queue>#include<vector>#include<map>#include<Set>#include<stack>#include<cmath>#include<cstdio>#include<algorithm>#include<iomanip>#include<stdlib.h>#include<time.h>#defineLL Long Longusing namespacestd;ConstLL mod =100001;ConstLL N =300010;ints1l, s2l;classsf{//N: Array size Public: intX[n], y[n], c[n]; intHeight[n],str[n], Sa[n], rank[n];//height array starting from 2 intSlen; intm=1050;//Character Set processing size (passing in if not a number, need to do displacement conversion) BOOLcmpint* R,intAintBintl) {returnR[a] = = R[b] && r[a + l] = = R[b +L]; } voidSuffix (intN) {++N; intI, J, p; for(i =0; I < m; ++i) C[i] =0; for(i =0; I < n; ++i) C[x[i] = str[i]]++; for(i =1; I < m; ++i) C[i] + = c[i-1]; for(i = n-1; I >=0; -i) sa[--c[x[i]] =i; for(j =1; J <= N; J <<=1) {p=0; for(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) C[i] =0; for(i =0; I < n; ++i) c[x[y[i]]]++; for(i =1; I < m; ++i) C[i] + = c[i-1]; for(i = n-1; I >=0; -i) sa[--c[x[y[i] []] =Y[i]; Swap (x, y); P=1; x[sa[0]] =0; for(i =1; I < n; ++i) {X[sa[i]]= CMP (y, Sa[i-1], Sa[i], j)? P-1: p++; } if(P >= N) Break; M=p; } intK =0; N--; for(i =0; I <= N; ++i) Rank[sa[i] =i; for(i =0; I < n; ++i) {if(k)--K; J= Sa[rank[i]-1]; while(Str[i + K] = = Str[j + K]) + +K; Height[rank[i]]=K; //cout << k << Endl; } } voidInitstringS//no matter what parameter, pass in with reference{Slen=s.length (); for(inti =0; i < Slen; i++) Str[i]=s[i]-'a'+2;//if it is a character, it is mapped to a sequence starting from 1Str[slen] =1;//0 as Terminator to prevent cross-borderSuffix (Slen); } LL Solve () {intAns =0; for(inti =2; I <= Slen; i++) { intSA1 = Sa[i-1],sa2=Sa[i]; if(SA1 >sa2) Swap (SA1, SA2); if(SA1 >= s1l | | SA2 < S1L)Continue; Ans= Max (ans, min (s1l-SA1, height[i]); } returnans; }}SF; LL dp[ *][2]; LL N;intMain () {Cin.sync_with_stdio (false); strings1,s2; while(Cin >> S1>>S2) {s1l=s1.length (); S2L=s2.length (); Sf.init (S1+S2); cout<< sf.solve () <<Endl; } return 0;}
hihocoder-1415 suffix Array Three • The longest common substring of a repeating melody of 32 strings