Title Link: Http://codeforces.com/problemset/problem/504/E
Test instructions: Give a tree, with a letter on each node. Each query gives two paths, asking the longest common prefix of the strings for both paths.
Idea: The tree chain, record each chain of strings, both positive and negative are recorded, forming a large string. Record the position of each chain corresponding to the string in the large string. The suffix array is then evaluated for the large string. The last question is the query on some links.
The tree chain is always so good to split.
const int N=600005;int next[n],node[n],head[n],e;void Add (int u,int v) {node[e]=v; Next[e]=head[u]; head[u]=e++;} int N;char s[n];int dep[n],sonnum[n],fa[n];void dfs (int u,int pre) {dep[u]=dep[pre]+1; Fa[u]=pre; Sonnum[u]=1; for (int i=head[u];i!=-1;i=next[i]) {int v=node[i]; if (v!=pre) {DFS (v,u); SONNUM[U]+=SONNUM[V]; }}}int root[n],end[n];void DFS (int u,int RT) {Root[u]=rt; End[rt]=u; int s=0; for (int i=head[u];i!=-1;i=next[i]) {int v=node[i]; if (Dep[v]>dep[u]&&sonnum[v]>sonnum[s]) {s=v; }} if (!s) return; DFS (S,RT); for (int i=head[u];i!=-1;i=next[i]) {int v=node[i]; if (dep[v]>dep[u]&&v!=s) {DFS (v,v); }}}int s[n];int snum;int p[n][4];struct sufarr{int r[n],sa[n],wa[n],wb[n],wd[n],rank[n],h[n]; int cmp (int *r,int a,int b,int len) {return r[a]==r[b]&&r[a+len]==r[b+len]; } void da (int *r,int *sa,int n,int m) {int i,j,p,*x=wa,*y=wb,*t; for (int i=0;i<m;i++) wd[i]=0; for (int i=0;i<n;i++) wd[x[i]=r[i]]++; for (int i=1;i<=m-1;i++) wd[i]+=wd[i-1]; for (int i=n-1;i>=0;i--) sa[--wd[x[i]]]=i; for (j=1,p=1;p<n;j<<=1,m=p) {p=0; for (int i=n-j;i<=n-1;i++) y[p++]=i; for (int i=0;i<n;i++) if (sa[i]>=j) y[p++]=sa[i]-j; for (int i=0;i<m;i++) wd[i]=0; for (int i=0;i<n;i++) wd[x[i]]++; for (int i=1;i<=m-1;i++) wd[i]+=wd[i-1]; for (int i=n-1;i>=0;i--) sa[--wd[x[y[i]]]]=y[i]; t=x;x=y;y=t;p=1;x[sa[0]]=0; for (int i=1;i<=n-1;i++) x[sa[i]]=cmp (y,sa[i-1],sa[i],j)? p-1:p++; }} void Calheight (int *r,int *sa,int n) {int i,j,k=0; for (int i=1;i<=n;i++) rank[sa[i]]=i; for (int i=0;i<n;i++) {if (k) k--; J=SA[RANK[I]-1]; while (I+k<n&&j+k<n&&r[i+k]==r[j+k]) k++; H[rank[i]]=k; }} int f[n][20],n; void init () {int i,j; for (int i=1;i<=n;i++) f[i][0]=h[i]; For (i=1;1+ (1<<i) <=n;i++) for (j=1;j+ (1<<i) -1<=n;j++) {f[j][i]=min (f[j][i-1],f[j+ (1< < (I-1))][i-1]); }} int cal (int a,int b) {if (a==b) return n-a; A=rank[a]; B=RANK[B]; if (a>b) swap (A, b); a++; int M=floor (log (1.0* (b-a+1))/log (2.0)); return min (f[a][m],f[b-(1<<m) +1][m]); }/**store:r[0~len-1] and r[i]>0 for all i**/void process (int *r,int len,int typenum) {N=len; r[len]=0; Da (r,sa,len+1,typenum); Calheight (R,sa,n); Init (); }}a;vector<pair<int,int> > init (int a,int b) {vector<pair<int,int> > pp,qq; while (Root[a]!=root[b]) {if (Dep[root[a]]>DEP[ROOT[B]]) {int rt=root[a]; int y=p[rt][1]; int x=y-(DEP[A]-DEP[RT]); PP.PB (MP (x, y)); A=FA[RT]; } else {int rt=root[b]; int x=p[rt][2]; int y=x+ (DEP[B]-DEP[RT]); QQ.PB (MP (x, y)); B=FA[RT]; }} if (Dep[a]>dep[b]) {int rt=root[a]; int e=p[rt][1]; int y=e-(DEP[B]-DEP[RT]); int x=e-(DEP[A]-DEP[RT]); PP.PB (MP (x, y)); } else {int rt=root[a]; int e=p[rt][2]; int y=e+ (DEP[B]-DEP[RT]); int x=e+ (DEP[A]-DEP[RT]); PP.PB (MP (x, y)); } for (int i=sz (QQ) -1;i>=0;i--) {PP.PB (qq[i]); } return pp;} int cal (int a,int b,int c,int d) {vector<pair<int,int> > P=init (A, b); vector<pair<int,int> > Q=init (c,d); int ans=0; int i=0,j=0; while (I<sz (p) &&j<sz (q)) {int len=a.cal (p[i].first,q[j].First); int tmp=min (p[i].second-p[i].first+1,q[j].second-q[j].first+1); int k=min (LEN,TMP); Ans+=k; if (len<tmp) break; if (k>=p[i].second-p[i].first+1) i++; else p[i].first+=k; if (k>=q[j].second-q[j].first+1) j + +; else q[j].first+=k; } return ans; int main () {CLR (head,-1); N=myint (); scanf ("%s", s+1); for (int i=1;i<n;i++) {int u=myint (); int V=myint (); Add (U,V); Add (V,u); } dfs (1,0); DFS (a); Snum=-1; for (int i=1;i<=n;i++) if (I==root[i]) {p[i][0]=snum+1; for (int k=end[i];; K=fa[k]) {s[++snum]=s[k]-' a ' +1; if (k==i) break; } P[i][1]=snum; p[i][2]=snum+1; for (int k=snum;k>=p[i][0];k--) {s[++snum]=s[k]; } P[i][3]=snum; } snum++; A.process (s,snum,30); int Q=myint (); while (q--) {int a=myint (); int B=myint (); Int C=myint (); int D=myint (); printf ("%d\n", Cal (A,b,c,d)); }}
CF 504EMisha and LCP on tree (tree chain + suffix array)