http://acm.hdu.edu.cn/showproblem.php?pid=1403
Longest Common Substring
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3068 Accepted Submission(s): 1087
Problem Description
Given two strings, you have to tell the length of the Longest Common Substring of them.
For example: str1 = banana str2 = cianaic
So the Longest Common Substring is "ana", and the length is 3.
For example: str1 = banana str2 = cianaic
So the Longest Common Substring is "ana", and the length is 3.
Input
The input contains several test cases. Each test case contains two strings, each string will have at most 100000 characters. All the characters are in lower-case.
Process to the end of file.
Process to the end of file.
Output
For each test case, you have to tell the length of the Longest Common Substring of them.
Sample Input
banana cianaic
Sample Output
3
思路:
构造后缀数组和Heigh数组,利用height数组和sa数组的性质求解参考
用后缀数组的另外一种实现方法,一直WA,下面我将AC代码和WA代码同时给出,求大神在WA代码给点意见
AC代码:
1 #include <iostream> 2 3 using namespace std; 4 5 #define maxn 1000001 6 #define cls(x) memset(x, 0, sizeof(x)) 7 int wa[maxn],wb[maxn],wv[maxn],wss[maxn]; 8 int cmp(int *r,int a,int b,int l) 9 {return r[a]==r[b]&&r[a+l]==r[b+l];} 10 11 void da(char *r,int *sa,int n,int m) 12 { 13 cls(wa); 14 cls(wb); 15 cls(wv); 16 cls(wss); 17 int i,j,p,*x=wa,*y=wb,*t; 18 for(i=0;i<m;i++) wss[i]=0; 19 for(i=0;i<n;i++) wss[x[i]=r[i]]++; 20 for(i=1;i<m;i++) wss[i]+=wss[i-1]; 21 for(i=n-1;i>=0;i--) sa[--wss[x[i]]]=i; 22 for(j=1,p=1;p<n;j*=2,m=p) 23 { 24 for(p=0,i=n-j;i<n;i++) y[p++]=i; 25 for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j; 26 for(i=0;i<n;i++) wv[i]=x[y[i]]; 27 for(i=0;i<m;i++) wss[i]=0; 28 for(i=0;i<n;i++) wss[wv[i]]++; 29 for(i=1;i<m;i++) wss[i]+=wss[i-1]; 30 for(i=n-1;i>=0;i--) sa[--wss[wv[i]]]=y[i]; 31 for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;i++) 32 x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++; 33 } 34 return; 35 } 36 int rank[maxn],height[maxn]; 37 void calheight(char *r,int *sa,int n) 38 { 39 cls(rank); 40 cls(height); 41 int i,j,k=0; 42 for(i=1;i<n;i++) rank[sa[i]]=i; 43 for(i=0;i<n;height[rank[i++]]=k) 44 for(k?k--:0,j=sa[rank[i]-1];r[i+k]==r[j+k]&&i!=j;k++); 45 return; 46 } 47 48 char ca[maxn * 2]; 49 int sa[maxn]; 50 51 int main() 52 { 53 while (cin >> ca) 54 { 55 int len = strlen(ca); 56 int lenstr1 = len; 57 ca[len] = '#'; 58 cin >> (ca + len + 1); 59 60 len = strlen(ca); 61 da(ca, sa, len, 130); 62 63 int i; 64 calheight(ca,sa,len); 65 66 int max = 0; 67 //cout << ca << endl; 68 for (i = 1; i < len; ++i) 69 { 70 if (height[i] > max) 71 { 72 if ((sa[i] > lenstr1 && sa[i - 1] < lenstr1) || (sa[i - 1] > lenstr1 && sa[i] < lenstr1)) 73 { 74 max = height[i]; 75 } 76 } 77 } 78 //for (int i = 0; i < len; ++i) 79 //cout << (ca + i) << endl; 80 cout << max << endl; 81 cls(ca); 82 } 83 return 0; 84 }
WA到死的代码:
1 #include<iostream> 2 #include<stdio.h> 3 #include<string.h> 4 #define MAXN 100010 5 6 char str[MAXN*2]; 7 int s[MAXN*2],sa[MAXN*2],rank[MAXN*2],trank[MAXN*4],sum[MAXN*4],tsa[MAXN*2],height[MAXN*2]; 8 9 void sorting(int j,int len)//基数排序 10 { 11 int i; 12 memset(sum,0,sizeof(sum)); 13 for (i=1; i<=len; i++) sum[ rank[i+j] ]++; 14 for (i=1; i<=len; i++) sum[i]+=sum[i-1]; 15 for (i=len; i>0; i--) tsa[ sum[ rank[i+j] ]-- ]=i;//对第二关键字计数排序,tsa代替sa为排名为i的后缀是tsa[i] 16 17 memset(sum,0,sizeof(sum)); 18 for (i=1; i<=len; i++) sum[ rank[i] ]++; 19 for (i=1; i<=len; i++) sum[i]+=sum[i-1]; 20 for (i=len; i>0; i--) sa[ sum[ rank[ tsa[i] ] ]-- ]= tsa[i]; //对第一关键字计数排序 21 //构造互逆关系 22 // for(i=1;i<=len;i++) printf("%d ",rank[i]); putchar(10); 23 } 24 25 void getsa(int len){ 26 27 memset(sum,0,sizeof(sum)); 28 memset(rank,0,sizeof(rank)); 29 memset(height,0,sizeof(height)); 30 memset(trank,0,sizeof(trank)); 31 memset(sa,0,sizeof(sa)); 32 memset(tsa,0,sizeof(tsa)); 33 34 int i; 35 for (i=0; i<len; i++) { 36 trank[i+1]=s[i]; 37 } 38 for (i=1; i<=len; i++) { 39 sum[ trank[i] ]++; 40 } 41 for (i=1; i<=40; i++) sum[i]+=sum[i-1]; 42 for (i=len; i>0; i--) sa[ sum[ trank[i] ]-- ]=i; 43 44 // for(i=1;i<=len;i++) printf("%d ",sa[i]);putchar(10); 45 46 rank[ sa[1] ]=1; 47 48 int p; 49 for (i=2,p=1; i<=len; i++) 50 { 51 if (trank[ sa[i] ]!=trank[ sa[i-1] ]) p++; 52 rank[ sa[i] ]=p; 53 }//第一次的sa与rank构造完成 54 55 //rank1: 11211112 56 57 // for(i=1;i<=len;i++) printf("%d ",rank[i]); putchar(10); 58 59 for (int j=1; j<=len; j*=2) 60 { 61 sorting(j,len); 62 trank[ sa[1] ]=1; 63 p=1; //用trank代替rank 64 for (i=2; i<=len; i++) 65 { 66 if ((rank[ sa[i] ]!=rank[ sa[i-1] ]) || (rank[ sa[i]+j ]!=rank[ sa[i-1]+j ])) p++; 67 trank[ sa[i] ]=p;//空间要开大一点,至少2倍 68 } 69 for (i=1; i<=len; i++) rank[i]=trank[i]; 70 } 71 } 72 73 void getheight(int len) 74 { 75 for (int i=1,j=0; i<=len; i++)//用j代替上面的h数组 76 { 77 if (rank[i]==1) continue; 78 for (; s[i+j-1]==s[ sa[ rank[i]-1 ]+j-1 ]; ) j++;//注意越界之类的问题 79 height[ rank[i] ]=j; 80 if (j>0) j--; 81 } 82 } 83 84 int main(){ 85 86 while(~scanf("%s",str)){ 87 int i; 88 int len1 = strlen(str); 89 str[len1]='#'; 90 for(i=0;i<len1;i++){ 91 s[i]=str[i]-'a'+1; 92 } 93 s[len1] = '#'; 94 scanf("%s",str+len1+1); 95 int maxlen = strlen(str); 96 for(i=len1+1;i<maxlen;i++){ 97 s[i]=str[i]-'a'+1; 98 } 99 s[maxlen]=0; 100 getsa(maxlen); 101 getheight(maxlen); 102 int maks = 0; 103 len1++; 104 for (i = 2; i <= maxlen; ++i) 105 { 106 if (height[i] > maks) 107 { 108 if ((sa[i] > len1 && sa[i - 1] < len1) || (sa[i - 1] > len1 && sa[i] < len1)) 109 { 110 maks = height[i]; 111 } 112 } 113 } 114 printf("%d\n",maks); 115 } 116 return 0; 117 }
无解WA,求大神指教!!!