Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 11195 | Accepted: 3701 |
Description
A substring of a string T is defined as:
Given two strings A, B and one integer K, we define S, a set of triples (i, 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| ≤ 105
1 ≤ 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
思路:每個子串可以看作是後綴的公共前綴,首先考慮針對每個a串的後綴,b串每個後綴的貢獻,用一個單調棧維護高度數組。兩個後綴之間的最長公共前綴是後綴數組中連續高度數組的最小值,因此高度數組的較大值可以被較小值覆蓋。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
#define F(x) ((x)/3+((x)%3==1?0:tb))
#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
using namespace std;
const int MAXN = 4e6+10;
const int N = MAXN;
const int INF=1e9+7;
int wa[MAXN],wb[MAXN],wv[MAXN],ws[MAXN];
char s[MAXN],t[MAXN];
int a[MAXN];
int sa[MAXN],height[MAXN],RANK[MAXN];
int len[MAXN];
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) //涵義與DA 相同
{
int i,j,*san=sa+n,ta=0,tb=(n+1)/3,tbc=0,p;
int *rn=r+n;
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 calheight(int *r,int *sa,int n)
{ // 此處N爲實際長度
int i,j,k=0;
// height[]的合法範圍爲 1-N, 其中0是結尾加入的字符
for(i=1;i<=n;i++)
RANK[sa[i]]=i;
// 根據SA求RANK
for(i=0;i<n; height[RANK[i++]] = k )
// 定義:h[i] = height[ RANK[i] ]
for(k?k--:0,j=sa[RANK[i]-1];
r[i+k]==r[j+k]; k++);
//根據 h[i] >= h[i-1]-1 來優化計算height過程
}
int k;
int n,l;
//q[0][i]代表當前的高度數組的值
//q[1][i]代表被當前的值覆蓋了幾個數
int q[2][MAXN];
void solve(){
long long res=0;
long long tot=0;
int top=0;
for(int i=1;i<=n;i++){
if(height[i]<k){
tot=0;
top=0;
continue;
}
long long cnt=0;
if(sa[i-1]<l){
cnt++;
tot+=1LL*height[i]-k+1;
}
while(top&&q[0][top]>height[i]){
tot-=1LL*q[1][top]*(1LL*q[0][top]-height[i]);
cnt+=q[1][top];
top--;
}
q[0][++top]=height[i];
q[1][top]=(int)cnt;
if(sa[i]>l)
res+=tot;
}
tot=0,top=0;
for(int i=1;i<=n;i++){
if(height[i]<k){
tot=0;
top=0;
continue;
}
long long cnt=0;
if(sa[i-1]>l){
cnt++;
tot+=1LL*height[i]-k+1;
}
while(top&&q[0][top]>height[i]){
tot-=1LL*q[1][top]*(1LL*q[0][top]-height[i]);
cnt+=q[1][top];
top--;
}
q[0][++top]=height[i];
q[1][top]=(int)cnt;
if(sa[i]<l)
res+=tot;
}
printf("%lld\n",res);
}
int main(){
while(scanf("%d",&k)&&k){
scanf("%s%s",s,t);
n=(int)strlen(s);
for(int i=0;i<n;i++){
a[i]=s[i];
}
a[n++]=1;
l=n-1;
int m=(int)strlen(t);
for(int i=0;i<m;i++){
a[n+i]=t[i];
}
n=n+m;
a[n]=0;
dc3(a,sa,n+1,200);
calheight(a, sa, n);
solve();
}
}