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

2014-11-24 10:34:58 · 作者: · 浏览: 1
or(i=0;i for(i=0;i for(i=1;i 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 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 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] else{
int cnt=0;
if(sa[i-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] 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] }
}
printf("%I64d\n",sum);
}
return 0;
}