Topic links
Test instructions: Input N (2 <= n <= 4000) length of not more than 200 string, the output of the smallest dictionary sequence of the longest common continuous substring;
Idea: Put all the strings in the middle of the delimiter, note: The delimiter only need and input characters are different, and different can be, there is no need to be the smallest character;
The connection suffix array solves the height after the two minutes length, because the height is based on the SA array, so the front conforms to the dictionary order of the smallest, directly find the stop can be;
PS: To simplify the previous template, a problem is the key;
#include <iostream>#include<cstdio>#include<cstring>#include<string.h>#include<algorithm>#include<map>#include<queue>#include<vector>#include<cmath>#include<stdlib.h>#include<time.h>using namespacestd;#defineMS0 (a) memset (A,0,sizeof (a))typedefLong Longll;Const intMAXN =1000005;intSA[MAXN],T[MAXN],T2[MAXN],C[MAXN],WV[MAXN];intcmpint*r,intAintBintl) { returnR[a] = = R[b] && r[a+l] = = r[b+l];}voidBuild_sa (int*r,intNintm) {//multiplication Algorithm R is a range of characters for which the array n to be matched is the total length m intI, J, p, *x = t, *y =T2; for(i =0; I < m; i + +) c[i] =0; for(i =0; I < n; i + +) c[x[i] = R[i]] + +; for(i =1; I < m; i + +) C[i] + = c[i-1]; for(i = n1; I >=0; I--) sa[--c[x[i]] =i; for(j =1, p =1; P < n; J <<=1, 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 < n; i + +) wv[i] =X[y[i]]; for(i =0; I < m; i + +) c[i] =0; for(i =0; I < n; i + +) C[wv[i]] + +; for(i =1; I < m; i + +) C[i] + = c[i-1]; for(i = n1; I >=0; I--) sa[--c[wv[i]] =Y[i]; for(Swap (x, y), 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++; } }}intRK[MAXN],HEIGHT[MAXN];voidGetHeight (int*r,intN) { for(inti =1; I <= n;i++) rk[sa[i]] = i;//Rk[i]: subscript of suffix I in sa[] for(inti =0, j,k =0; I < n; height[rk[i++]] =k) { for(K. k--:0, j = Sa[rk[i]-1];r[i+k] = = r[j+k];k++); }}intd[maxn],num[maxn],vs[4444],t,len,tot;Chartt[222];BOOLCheckintL) {MS0 (VS); intCNT =0; for(inti =2; I <= tot;i++){ if(Height[i] <L) {MS0 (VS); CNT=0; Continue; } if(!Vs[d[sa[i]]) {Vs[d[sa[i] ]=1; cnt++; } if(!vs[d[sa[i-1]] ) {vs[d[sa[i-1]]] =1; cnt++; } if(cnt = = T) {//for the same length, we only take the first occurrence of the strings that match the criteria; for(intj =0; j<l; J + +) Tt[j]= num[sa[i]+J]; TT[L]=' /'; return true; } } return false;}Charstr[222];intMain () { while(SCANF ("%d", &t) = =1&&T) {Tot=0; for(inti =1; I <= t;i++) {scanf ("%s", str); Len=strlen (str); for(intj =0; J < len;j++) Num[tot]= str[j],d[tot++] =i; Num[tot]='Z'+i,d[tot++] ='Z'+i;//' # ' + I turned up WA} Num[tot]=0;//Finally, add a minimum character;Build_sa (num,tot+1,5155); GetHeight (Num,tot); intAns =0, L =1, r =Len,mid,id; while(L <=R) {Mid= L + R >>1; if(Check (mid)) {ans = Mid,l = mid +1;} ElseR = Mid-1; } if(ans) {printf ("%s\n", TT); } ElsePuts"IDENTITY LOST"); } return 0;}
POJ 3518 Corporate Identity suffix array-string longest identical continuous substring