先上题目:
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4010 Accepted Submission(s): 1510
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define MAX 100002 5 using namespace std; 6 7 char s[(MAX<<1)],b[MAX]; 8 int sa[MAX<<1],rank[MAX<<1],height[MAX<<1],t[MAX<<1],t2[MAX<<1],c[MAX<<1],n,li; 9 int f[(MAX<<1)]; 10 11 void build_sa(int m){ 12 int i,*x=t,*y=t2; 13 for(i=0;i<m;i++) c[i]=0; 14 for(i=0;i<n;i++) c[x[i]=s[i]]++; 15 for(i=0;i<m;i++) c[i]+=c[i-1]; 16 for(i=n-1;i>=0;i--) sa[--c[x[i]]]=i; 17 for(int k=1;k<=n;k<<=1){ 18 int p=0; 19 for(i=n-k;i<n;i++) y[p++]=i; 20 for(i=0;i<n;i++) if(sa[i]>=k) y[p++]=sa[i]-k; 21 for(i=0;i<m;i++) c[i]=0; 22 for(i=0;i<n;i++) c[x[y[i]]]++; 23 for(i=0;i<m;i++) c[i]+=c[i-1]; 24 for(i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i]; 25 swap(x,y); 26 p=1; x[sa[0]]=0; 27 for(i=1;i<n;i++){ 28 x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k] ? p-1 : p++; 29 } 30 if(p>=n) break; 31 m=p; 32 } 33 for(i=0;i<n;i++) rank[sa[i]]=i; 34 } 35 36 void getHeight(){ 37 int i,j,k=0; 38 for(i=0;i<n;i++){ 39 if(k) k--; 40 j=sa[rank[i]-1]; 41 while(s[i+k]==s[j+k]) k++; 42 height[rank[i]]=k; 43 } 44 } 45 46 int main() 47 { 48 int maxn; 49 //freopen("data.txt","r",stdin); 50 while(scanf("%s %s",s,b)!=EOF){ 51 strcat(s,"&"); 52 li=strlen(s); 53 for(int i=0;i<li-1;i++) f[i]=1; 54 strcat(s,b); 55 f[li-1]=0; 56 n=strlen(s); 57 for(int i=li;i<n;i++) f[i]=-1; 58 build_sa(200); 59 getHeight(); 60 maxn=0; 61 for(int i=1;i<n;i++){ 62 if(maxn<height[i] && f[sa[i-1]]*f[sa[i]]<0){ 63 maxn=height[i]; 64 } 65 } 66 printf("%d\n",maxn); 67 } 68 return 0; 69 }
HDU - 1403 - Longest Common Substring,布布扣,bubuko.com
HDU - 1403 - Longest Common Substring
原文:http://www.cnblogs.com/sineatos/p/3892495.html