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

该博客详细介绍了如何利用后缀数组和单调栈解决POJ 3415问题,即找出长度不小于k的公共子串个数。作者提出将两个字符串通过特殊字符连接,并通过height数组分组,再借助单调栈优化,降低时间复杂度到O(n),实现高效的公共子串统计。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载请注明出处,谢谢http://blog.csdn.net/acm_cxlove/article/details/7854526       by---cxlove 

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

http://poj.org/problem?id=3415 

继续论文上的题目。

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

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

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

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

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

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

然后扫描两遍即可

#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;
}
	


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值