Topic links
BZOJ3230
Permission questions
Exercises
Suffix array base problem
Ask for k large different substring and positive LCP length
If you re, you need to know that the input of the inquiry will explode ll
#include <algorithm>#include <iostream>#include <cstring>#include <cstdio>#include <cmath>#include <map>#define Redge (U) for (int k = h[u],to; k; k = ed[k].nxt)#define REP (i,n) for (int i = 1; I <= (n); i++)#define MP ( A, b) make_pair<int,int> (A, B)#define CLS (s) memset (s,0,sizeof (s) )#define CP pair<int,int>#define LL Long Long intusing namespaceStdConst intMAXN =400005, MAXM =100005, INF =1000000000;inlinell read () {ll out =0, flag =1;Charc = GetChar (); while(C < -|| C > $){if(c = ='-') flag =-1; c = GetChar ();} while(c >= -&& C <= $) {out = (out <<3) + (out <<1) + C- -; c = GetChar ();}returnOut * FLAG;}intn,m,bac[maxn],t1[maxn],t2[maxn],bin[ -],LOG[MAXN]; LL SUM[MAXN];structsa{CharS[MAXN];intsa[maxn],rank[maxn],height[maxn],mn[maxn][ -];voidGetsa () {int*x = T1,*y = t2; m =255; for(inti =0; I <= m; i++) Bac[i] =0; for(inti =1; I <= N; i++) Bac[x[i] = s[i]]++; for(inti =1; I <= m; i++) Bac[i] + = bac[i-1]; for(inti = n; I i--) sa[bac[x[i]]--] = i; for(intK =1; K <= N; K <<=1){intp =0; for(inti = N-k +1; I <= N; i++) y[++p] = i; for(inti =1; I <= N; i++)if(Sa[i]-k >0) y[++p] = sa[i]-K; for(inti =0; I <= m; i++) Bac[i] =0; for(inti =1; I <= N; i++) bac[x[y[i]]]++; for(inti =1; I <= m; i++) Bac[i] + = bac[i-1]; for(inti = n; I i--) sa[bac[x[y[i]]]--] = Y[i]; Swap (x, y); x[sa[1]] = p =1; for(inti =2; I <= N; i++) X[sa[i] = (Y[sa[i]] = = Y[sa[i-1]] && Y[sa[i] + K] = = y[sa[i-1] + K]? P: ++p);if(P >= N) Break; m = p; } for(inti =1; I <= N; i++) Rank[sa[i]] = i; for(inti =1, k =0; I <= N; i++) {if(k) k--;intj = Sa[rank[i]-1]; while(S[i + K] = = S[j + K]) k++; Height[rank[i]] = k; } for(inti =1; I <= N; i++) mn[i][0] = Height[i]; for(intj =1; J <= -; J + +) for(inti =1; I <= N; i++) {if(i + bin[j]-1> N) Break; Mn[i][j] = min (Mn[i][j-1],mn[i + bin[j-1]][j-1]); } }intLcpintXintY) {if(x = = y)returnN-x +1;intL = Rank[x],r = Rank[y];if(L > R) swap (L,R); l++;intt = log[r-l +1];returnMin (Mn[l][t],mn[r-bin[t] +1][t]); }}l,r;voidInit () {sum[1] = n-l.sa[1] +1; for(inti =2; I <= N; i++) Sum[i] = sum[i-1] + (N-l.sa[i] +1)-l.height[i];}intQvoidSolve () {LL f,a,b,x,y;intL1,R1,L2,R2; while(q--) {x = read (); y = read ();if(x > Sum[n] | | y > sum[n]) {Puts ("-1");Continue;}intp = lower_bound (sum +1, Sum +1+ n,x)-sum;intQ = lower_bound (sum +1, Sum +1+ n,y)-sum;if(Sum[p] = = x) L1 = L.SA[P],R1 = n;ElseL1 = L.SA[P],R1 = N-(sum[p]-X);if(Sum[q] = = y) L2 = L.SA[Q],R2 = n;ElseL2 = L.SA[Q],R2 = N-(sum[q]-y); A = Min (L.LCP (l1,l2), min (R1-l1 +1, R2-l2 +1)); b = min (R.LCP (N-R1 +1, N-R2 +1), min (R1-l1 +1, R2-l2 +1)); f = A * a + b * b; printf"%lld\n", f); }}intMain () {bin[0] =1; for(inti =1; I <= -; i++) Bin[i] = bin[i-1] <<1; log[0] =-1; for(inti =1; I <=100000; i++) Log[i] = Log[i >>1] +1; n = read (); Q = Read (); scanf"%s", L.s +1); for(inti =1; I <= N; i++) R.s[i] = L.s[n-i +1]; L.getsa (); R.getsa (); Init (); Solve ();return 0;}
BZOJ3230 similar substring "suffix array"