POJ 3415 Common Substrings(后缀数组+单调栈)

来源:岁月联盟 编辑:exp 时间:2012-09-06

题目:求出长度不小于k的公共子串个数

 


继续论文上的题目。

计算A的某个后缀与B的某个后缀的最长公共前缀长度,如果长度L大于k,则加上L-k+1组。

将两个字符串连接起来,中间用一个没有出现的字符分开。(这是一个神奇的做法)

然后通过height数组分组,某个组内的height都是大于等于k的,也就是任意两个后缀的最长公共前缀都至少为k。

扫描一遍,遇到一个B的后缀就与之前的A后缀进行统计,求出所有的满足的组数。但是这样的做法便是n^2的。

可以发现两个后缀的最长公共前缀为这一段的height值的最小值。

可以通过一个单调栈来维护一下,当前要入栈元素如果小于栈底元素,说明之后加入的B后缀与栈底的最长公共前缀是小于等于入栈的。这样就保证了单调栈内的height值是绝对递增的,逐渐合并,均摊可以达到o(n)的复杂度。

然后扫描两遍即可


[cpp]
#include<iostream>  
#include<cstdio>  
#include<cstring>  
#include<algorithm>  
#include<cmath>  
#define N 100005  
#define LL long long  
#define maxn 200005  
using namespace std; 
//以下为倍增算法求后缀数组    
int wa[maxn],wb[maxn],wv[maxn],Ws[maxn];   
int cmp(int *r,int a,int b,int l)   
{return r[a]==r[b]&&r[a+l]==r[b+l];}   
void da(const char *r,int *sa,int n,int m){   
    int i,j,p,*x=wa,*y=wb,*t;    
    for(i=0;i<m;i++) Ws[i]=0;    
    for(i=0;i<n;i++) Ws[x[i]=r[i]]++;    
    for(i=1;i<m;i++) Ws[i]+=Ws[i-1];    
    for(i=n-1;i>=0;i--) sa[--Ws[x[i]]]=i;    
    for(j=1,p=1;p<n;j*=2,m=p){    
        for(p=0,i=n-j;i<n;i++) y[p++]=i;    
        for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;    
        for(i=0;i<n;i++) wv[i]=x[y[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--) sa[--Ws[wv[i]]]=y[i];    
        for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;i++)    
            x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;    
    }    
    return;    
}   
int sa[maxn],Rank[maxn],height[maxn];   
//求height数组    
void calheight(const char *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;   

char str[maxn],ch[maxn]; 
int k; 
int s[maxn][2]; 
LL tot,top; 
int main(){ 
    while(scanf("%d",&k)!=EOF&&k){ 
        int l1,l2; 
        scanf("%s%s",str,ch); 
        l1=strlen(str);l2=strlen(ch); 
        str[l1]='@'; 
        for(int i=l1+1;i<=l1+l2;i++) 
            str[i]=ch[i-l1-1]; 
        int n=l1+l2+1; 
        str[n]='/0'; 
        da(str,sa,n+1,130); 
        calheight(str,sa,n); 
        tot=top=0; 
        LL sum=0; 
        for(int i=1;i<=n;i++){ 
            if(height[i]<k) top=tot=0; 
            else{ 
                int cnt=0; 
                if(sa[i-1]<l1) cnt++,tot+=height[i]-k+1; 
                while(top>0&&height[i]<=s[top-1][0]){ 
                    top--; 
                    tot-=s[top][1]*(s[top][0]-height[i]); 
                    cnt+=s[top][1]; 
                } 
                s[top][0]=height[i];s[top++][1]=cnt; 
                if(sa[i]>l1) sum+=tot; 
            } 
        } 
        tot=top=0; 
        for(int i=1;i<=n;i++){ 
            if(height[i]<k) top=tot=0; 
            else{ 
                int cnt=0; 
                if(sa[i-1]>l1) cnt++,tot+=height[i]-k+1; 
                while(top>0&&height[i]<=s[top-1][0]){ 
                    top--; 
                    tot-=s[top][1]*(s[top][0]-height[i]); 
                    cnt+=s[top][1]; 
                } 
                s[top][0]=height[i];s[top++][1]=cnt; 
                if(sa[i]<l1) sum+=tot; 
            } 
        } 
        printf("%I64d/n",sum); 
    } 
    return 0; 

     

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define N 100005
#define LL long long
#define maxn 200005
using namespace std;
//以下为倍增算法求后缀数组 
int wa[maxn],wb[maxn],wv[maxn],Ws[maxn]; 
int cmp(int *r,int a,int b,int l) 
{return r[a]==r[b]&&r[a+l]==r[b+l];} 
void da(const char *r,int *sa,int n,int m){ 
 int i,j,p,*x=wa,*y=wb,*t;  
 for(i=0;i<m;i++) Ws[i]=0;  
 for(i=0;i<n;i++) Ws[x[i]=r[i]]++;  
 for(i=1;i<m;i++) Ws[i]+=Ws[i-1];  
 for(i=n-1;i>=0;i--) sa[--Ws[x[i]]]=i;  
 for(j=1,p=1;p<n;j*=2,m=p){  
  for(p=0,i=n-j;i<n;i++) y[p++]=i;  
  for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;  
  for(i=0;i<n;i++) wv[i]=x[y[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--) sa[--Ws[wv[i]]]=y[i];  
  for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;i++)  
   x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;  
 }  
 return;  

int sa[maxn],Rank[maxn],height[maxn]; 
//求height数组 
void calheight(const char *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; 
}
char str[maxn],ch[maxn];
int k;
int s[maxn][2];
LL tot,top;
int main(){
 while(scanf("%d",&k)!=EOF&&k){
  int l1,l2;
  scanf("%s%s",str,ch);
  l1=strlen(str);l2=strlen(ch);
  str[l1]='@';
  for(int i=l1+1;i<=l1+l2;i++)
   str[i]=ch[i-l1-1];
  int n=l1+l2+1;
  str[n]='/0';
  da(str,sa,n+1,130);
  calheight(str,sa,n);
  tot=top=0;
  LL sum=0;
  for(int i=1;i<=n;i++){
   if(height[i]<k) top=tot=0;
   else{
    int cnt=0;
    if(sa[i-1]<l1) cnt++,tot+=height[i]-k+1;
    while(top>0&&height[i]<=s[top-1][0]){
     top--;
     tot-=s[top][1]*(s[top][0]-height[i]);
     cnt+=s[top][1];
    }
    s[top][0]=height[i];s[top++][1]=cnt;
    if(sa[i]>l1) sum+=tot;
   }
  }
  tot=top=0;
  for(int i=1;i<=n;i++){
   if(height[i]<k) top=tot=0;
   else{
    int cnt=0;
    if(sa[i-1]>l1) cnt++,tot+=height[i]-k+1;
    while(top>0&&height[i]<=s[top-1][0]){
     top--;
     tot-=s[top][1]*(s[top][0]-height[i]);
     cnt+=s[top][1];
    }
    s[top][0]=height[i];s[top++][1]=cnt;
    if(sa[i]<l1) sum+=tot;
   }
  }
  printf("%I64d/n",sum);
 }
 return 0;
}