poj 3415Common Substrings

    技术2022-05-19  22

    Common Substrings Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 3112 Accepted: 1002

    Description

    A substring of a string T is defined as:

     

    T( i,  k)= TiTi +1... Ti+k -1, 1≤ i≤ i+k-1≤| T|.

     

    Given two strings A, B and one integer K, we define S, a set of triples (i, j, k):

     

    S = {( i,  j,  k) |  k≥ K,  A( i,  k)= B( j,  k)}.

     

    You are to give the value of |S| for specific A, B and K.

    Input

    The input file contains several blocks of data. For each block, the first line contains one integer K, followed by two lines containing strings A and B, respectively. The input file is ended by K=0.

    1 ≤ |A|, |B| ≤ 1051 ≤ K ≤ min{|A|, |B|}Characters of A and B are all Latin letters.

     

    Output

    For each case, output an integer |S|.

    Sample Input

    2 aababaa abaabaa 1 xx xx 0

    Sample Output

    22 5

    Source

    POJ Monthly--2007.10.06, wintokk

     

    /* 感谢qqz003大牛 经过一番折腾,总算是明白栈扫描这东西了 dc3还是比较犀利了,接着就刷进第一版了 */ #include<cstdio> #include<cstring> const int maxn=202000; int ws[maxn],wa[maxn],wb[maxn],wv[maxn]; int sa[maxn*5],height[maxn*5]; int rank[maxn],a[maxn]; char s[maxn]; int wstack[maxn][2]; #define F(x) ((x)/3+((x)%3==1?0:tb)) #define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2) int c0(int *r,int a,int b) { return r[a]==r[b]&&r[a+1]==r[b+1]&&r[a+2]==r[b+2]; } int c12(int k,int *r,int a,int b) { if(k==2) return r[a]<r[b]||r[a]==r[b]&&c12(1,r,a+1,b+1); else return r[a]<r[b]||r[a]==r[b]&&wv[a+1]<wv[b+1]; } void sort(int *r,int *a,int *b,int n,int m) { int i; for(i=0; i<n; i++) wv[i]=r[a[i]]; for(i=0; i<m; i++) ws[i]=0; for(i=0; i<n; i++) ws[wv[i]]++; for(i=1; i<m; i++) ws[i]+=ws[i-1]; for(i=n-1; i>=0; i--) b[--ws[wv[i]]]=a[i]; return; } void dc3(int *r,int *sa,int n,int m) { int i,j,*rn=r+n,*san=sa+n,ta=0,tb=(n+1)/3,tbc=0,p; r[n]=r[n+1]=0; for(i=0; i<n; i++) if(i%3!=0) wa[tbc++]=i; sort(r+2,wa,wb,tbc,m); sort(r+1,wb,wa,tbc,m); sort(r,wa,wb,tbc,m); for(p=1,rn[F(wb[0])]=0,i=1; i<tbc; i++) rn[F(wb[i])]=c0(r,wb[i-1],wb[i])?p-1:p++; if(p<tbc) dc3(rn,san,tbc,p); else for(i=0; i<tbc; i++) san[rn[i]]=i; for(i=0; i<tbc; i++) if(san[i]<tb) wb[ta++]=san[i]*3; if(n%3==1) wb[ta++]=n-1; sort(r,wb,wa,ta,m); for(i=0; i<tbc; i++) wv[wb[i]=G(san[i])]=i; for(i=0,j=0,p=0; i<ta && j<tbc; p++) sa[p]=c12(wb[j]%3,r,wa[i],wb[j])?wa[i++]:wb[j++]; for(; i<ta; p++) sa[p]=wa[i++]; for(; j<tbc; p++) sa[p]=wb[j++]; return; } void cal(int *r,int *sa,int n) { int i,j,k=0; for (i=1; i<=n; i++) rank[sa[i]]=i; for (i=0; i<n; height[rank[i++]]=k) for (k?k--:0,j=sa[rank[i]-1]; r[i+k]==r[j+k]; k++); return; } int main() { int k; int len1,len2,n; while(scanf("%d",&k)!=EOF) { if(k==0) break; scanf("%s",s); len1=strlen(s); for(int i=0;i<len1;++i) a[i]=(int)s[i]; a[len1]=1; scanf("%s",s); len2=strlen(s); for(int i=0;i<len2;++i) a[len1+1+i]=(int)s[i]; a[len1+len2+1]=0; n=len1+len2+1; dc3(a,sa,n+1,128); cal(a,sa,n); //栈扫描 for(int i=1;i<=n;i++)//初始化,只需要满足前缀大于k的即可 { height[i]-=k-1; if(height[i]<0) height[i]=0; } int top=0; long long tsum=0,sum=0; int w; for(int i=1;i<=n;i++) { w=0; while(top>0&&wstack[top-1][0]>height[i]) { top--; tsum+=(height[i]-wstack[top][0])*wstack[top][1];//更新 w+=wstack[top][1];//压缩 } wstack[top][0]=height[i];//压缩 wstack[top++][1]=w; if(sa[i-1]<len1)//还在A串,就加入这个后缀 { tsum+=height[i]; wstack[top-1][1]++; } if(sa[i]>len1)//现在讨论B串,到了一个有B串的,把前面的加起来 sum+=tsum; } top=0; tsum=0; for(int i=1;i<=n;i++) { w=0; while(top>0&&wstack[top-1][0]>height[i]) { top--; tsum+=(height[i]-wstack[top][0])*wstack[top][1];//更新 w+=wstack[top][1];//压缩 } wstack[top][0]=height[i];//压缩 wstack[top++][1]=w; if(sa[i-1]>len1)//还在B串 { tsum+=height[i]; wstack[top-1][1]++; } if(sa[i]<len1)//现在讨论A串,到了一个有A串的,把前面的加起来 { sum+=tsum; } } printf("%lld/n",sum); } return 0; }  


    最新回复(0)