| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
个人想到的后缀数组解法
1.把串b接到串a后面,在两串之间加上一个字符'$',得到串s
2.用后缀数组求s的height数组
3.对于每对后缀i,j,如果它们分属于a和b,且最长公共后缀长为p,则答案加上p-k+1
4.输出3中得到的答案
对于第三步,大概的做法是维护一个h[i]严格单调的序列,可做到均摊复杂度O(n),
可用如下代码实现
long long solve(int l1,int l2,int len,int k){
long long ans=0,w1=0,w2=0;
for(int i=2;i<=len;i++)h[i]=max(0,h[i]-k+1);
for(int i=2,p=0;i<=len;i++){
w[++p]=h[i];
if(sa[i-1]<=l1)pre[p]=1,post[p]=0,w1+=h[i];
else pre[p]=0,post[p]=1,w2+=h[i];
while(p>1&&w[p]<=w[p-1]){
w1-=pre[p-1]*(w[p-1]-w[p]),w2-=post[p-1]*(w[p-1]-w[p]);
w[p-1]=w[p];
pre[p-1]+=pre[p],post[p-1]+=post[p];
p--;
}
if(sa[i]<=l1)ans+=w2;
else ans+=w1;
}
return ans;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator