Concatenate two strings with a special character, then find the maximum height, and require that the first character of the two adjacent suffixes not be in the same string.
1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 6 Const intMAXN =200000+Ten;7 8 CharS[MAXN];9 intN;Ten intSA[MAXN], RANK[MAXN], HEIGHT[MAXN]; One intT[MAXN], T2[MAXN], C[MAXN]; A - voidBuild_sa (intNintm) - { the intI, *x = t, *y =T2; - for(i =0; I < m; i++) C[i] =0; - for(i =0; I < n; i++) C[x[i] = s[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(intK =1; K <= N; K <<=1) + { A intp =0; at for(i = n-k; i < n; i++) y[p++] =i; - for(i =0; I < n; i++)if(Sa[i] >= k) y[p++] = Sa[i]-K; - 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]; in swap (x, y); -p =1; x[sa[0]] =0; to for(i =1; I < n; i++) +X[sa[i]] = y[sa[i]]==y[sa[i-1]] && y[sa[i]+k]==y[sa[i-1]+K]? Ns1: p++; - if(P >= N) Break; them =p; * } $ }Panax Notoginseng - voidbuild_height () the { + intK =0; A for(inti =1; I <= N; i++) Rank[sa[i] =i; the for(inti =0; I < n; i++) + { - if(k) k--; $ intj = Sa[rank[i]-1]; $ while(S[i + K] = = S[j + K]) k++; -Height[rank[i]] =K; - } the } - Wuyi intMain () the { - //freopen ("In.txt", "R", stdin); Wu -scanf"%s", s); About intpos =strlen (s); $S[pos] =1; -scanf"%s", S + pos +1); -n =strlen (s); - ABUILD_SA (n +1, the); + build_height (); the - intAns =0; $ for(inti =2; I <= N; i++) the { the if(Height[i] >ans) the { the if((Sa[i]>pos && sa[i-1]<pos) | | (sa[i]<pos&&sa[i-1]>POS)) -Ans =Height[i]; in } the } the Aboutprintf"%d\n", ans); the the return 0; the}
code June
POJ 2774 (suffix array longest common string) long long Message