1 /******************************************************************2 title: Long Long Message (POJ 2774)3 Links:http://poj.org/problem?id=27744 Test instructions: Give two strings, find the longest common substring5 algorithm: suffix array6 algorithm idea: suffix array is the set template to get the first deserved array, this problem uses two arrays, divided7 Sa[],height[];sa[i] Indicates that all suffixes are sorted by dictionary number to s[i]8 The beginning suffix is ranked in the first position. Height[i] Indicates the number of dictionaries for the I and i+1 suffixes9 The longest string of prefixes. Ten *******************************************************************/ One#include <cstdio> A#include <cstring> -#include <algorithm> - using namespacestd; the - Const intmx=200100; - CharST[MX]; - intS[mx],sa[mx],t[mx],t2[mx],c[mx],n; + intRANK[MX],HEIGHT[MX]; - + voidBuild_sa (intm) A { at 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) in { - intp=0; to 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; the 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];Panax Notoginseng swap (x, y); -p=1; thex[sa[0]]=0; + for(i=1; i<n;i++) Ax[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k]?p-1:p + +; the if(p>=n) Break; +m=p; - } $ } $ - voidgetheight () - { the inti,j,k=0; - for(i=0; i<n;i++) rank[sa[i]]=i;Wuyi for(i=0; i<n-1; i++) the { - if(k) k--; Wu intj=sa[rank[i]-1]; - while(S[i+k]==s[j+k]) k++; Aboutheight[rank[i]]=K; $ } - } - - intMain () A { +n=0; the intL1,l2; -scanf"%s", ST); $l1=strlen (ST); the for(intI=0; i<l1;i++) s[n++]=st[i]-'a'+1; thes[n++]= -; thescanf"%s", ST); theL2=strlen (ST); - for(intI=0; i<l2;i++) s[n++]=st[i]-'a'+1; ins[n++]=0; theBuild_sa ( -); the getheight (); About intmaxx=0; the for(intI=2; i<n;i++) the { the if(maxx<Height[i]) + { - if(sa[i]>=0&&sa[i]<l1&&sa[i-1]>L1) themaxx=Height[i];Bayi if(sa[i-1]>=0&&sa[i-1]<l1&&sa[i]>L1) themaxx=Height[i]; the } - } -printf"%d\n", Maxx); the return 0; the}
POJ 2774 Long long Message (suffix array entry)