poj 3415 Common Substrings【SA+单调栈】
把两个串中间加一个未出现字符接起来,然后求SA
然后把贡献统计分为两部分,在排序后的后缀里,属于串2的后缀和排在他前面属于串1的后缀的贡献和属于串1的后缀和排在他前面属于串2的后缀的贡献
两部分分别作,都用单调栈维护一段里的height最小值(因为lcp是排序后两后缀中间height最小值),然后根据每次入栈种类来给答案算贡献
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=500005;
int n,m,k,wa[N],wb[N],wv[N],wsu[N],sa[N],rk[N],he[N],s[N],v[N],top;
long long tot,ans;
char c[N],t[N];
bool cmp(int r[],int a,int b,int l)
{
return r[a]==r[b]&&r[a+l]==r[b+l];
}
void saa(char r[],int n,int m)
{
int *x=wa,*y=wb;
for(int i=0;i<=m;i++)
wsu[i]=0;
for(int i=1;i<=n;i++)
wsu[x[i]=r[i]]++;
for(int i=1;i<=m;i++)
wsu[i]+=wsu[i-1];
for(int i=n;i>=1;i--)
sa[wsu[x[i]]--]=i;
for(int j=1,p=1;j<n&&p<n;j<<=1,m=p)
{
p=0;
for(int i=n-j+1;i<=n;i++)
y[++p]=i;
for(int i=1;i<=n;i++)
if(sa[i]>j)
y[++p]=sa[i]-j;
for(int i=1;i<=n;i++)
wv[i]=x[y[i]];
for(int i=0;i<=m;i++)
wsu[i]=0;
for(int i=1;i<=n;i++)
wsu[wv[i]]++;
for(int i=1;i<=m;i++)
wsu[i]+=wsu[i-1];
for(int i=n;i>=1;i--)
sa[wsu[wv[i]]--]=y[i];
swap(x,y);
p=1;
x[sa[1]]=1;
for(int i=2;i<=n;i++)
x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p:++p;
}
for(int i=1;i<=n;i++)
rk[sa[i]]=i;
for(int i=1,j,k=0;i<=n;he[rk[i++]]=k)
for(k?k--:0,j=sa[rk[i]-1];r[i+k]==r[j+k];k++);
}
int main()
{
while(scanf("%d",&k)&&k)
{
scanf("%s%s",c+1,t+1);
n=strlen(c+1),m=strlen(t+1);
c[n+1]=1;
for(int i=n+2;i<=n+m+1;i++)
c[i]=t[i-n-1];
saa(c,n+m+1,200);
sa[0]=n+m+2;
// for(int i=0;i<=n+m+2;i++)
// cerr<<sa[i]-1<<" ";
// cerr<<endl;
ans=0,top=0,tot=0;
for(int i=1;i<=n+m+1;i++)
{
if(he[i]<k)
top=0,tot=0;
else
{
int con=0;
if(sa[i-1]<=n)
con++,tot+=he[i]-k+1;
while(top>0&&he[i]<=s[top])
tot-=v[top]*(s[top]-he[i]),con+=v[top--];
s[++top]=he[i],v[top]=con;
if(sa[i]>n+1)
ans+=tot;//cerr<<tot<<endl;
}
}//cerr<<ans<<endl;
top=0,tot=0;
for(int i=1;i<=n+m+1;i++)
{
if(he[i]<k)
top=0,tot=0;
else
{
int con=0;
if(sa[i-1]>n+1)
con++,tot+=he[i]-k+1;
while(top>0&&he[i]<=s[top])
tot-=v[top]*(s[top]-he[i]),con+=v[top--];
s[++top]=he[i],v[top]=con;
if(sa[i]<=n)
ans+=tot;
}
}
printf("%lld\n",ans);
}
return 0;
}
poj 3415 Common Substrings【SA+单调栈】的更多相关文章
- 【POJ3415】 Common Substrings (SA+单调栈)
这道是求长度不小于 k 的公共子串的个数...很不幸,我又TLE了... 解法参考论文以及下面的链接 http://www.cnblogs.com/vongang/archive/2012/11/20 ...
- POJ 3415 Common Substrings(后缀数组 + 单调栈)题解
题意: 给两个串\(A.B\),问你长度\(>=k\)的有几对公共子串 思路: 先想一个朴素算法: 把\(B\)接在\(A\)后面,然后去跑后缀数组,得到\(height\)数组,那么直接\(r ...
- poj 3415 Common Substrings(后缀数组+单调栈)
http://poj.org/problem?id=3415 Common Substrings Time Limit: 5000MS Memory Limit: 65536K Total Sub ...
- poj 3415 Common Substrings - 后缀数组 - 二分答案 - 单调栈
题目传送门 传送点I 传送点II 题目大意 给定串$A, B$,求$A$和$B$长度大于等于$k$的公共子串的数量. 根据常用套路,用一个奇怪的字符把$A$,$B$连接起来,然后二分答案,然后按mid ...
- POJ 3415 Common Substrings(长度不小于K的公共子串的个数+后缀数组+height数组分组思想+单调栈)
http://poj.org/problem?id=3415 题意:求长度不小于K的公共子串的个数. 思路:好题!!!拉丁字母让我Wa了好久!!单调栈又让我理解了好久!!太弱啊!! 最简单的就是暴力枚 ...
- poj 3415 Common Substrings——后缀数组+单调栈
题目:http://poj.org/problem?id=3415 因为求 LCP 是后缀数组的 ht[ ] 上的一段取 min ,所以考虑算出 ht[ ] 之后枚举每个位置作为右端的贡献. 一开始想 ...
- poj 3415 Common Substrings —— 后缀数组+单调栈
题目:http://poj.org/problem?id=3415 先用后缀数组处理出 ht[i]: 用单调栈维护当前位置 ht[i] 对之前的 ht[j] 取 min 的结果,也就是当前的后缀与之前 ...
- poj 3415 Common Substrings 后缀数组+单调栈
题目链接 题意:求解两个字符串长度 大于等于k的所有相同子串对有多少个,子串可以相同,只要位置不同即可:两个字符串的长度不超过1e5; 如 s1 = "xx" 和 s2 = &qu ...
- POJ - 3415 Common Substrings(后缀数组求长度不小于 k 的公共子串的个数+单调栈优化)
Description A substring of a string T is defined as: T( i, k)= TiTi+1... Ti+k-1, 1≤ i≤ i+k-1≤| T|. G ...
随机推荐
- Android开发Tips(3)
欢迎Follow我的GitHub, 关注我的CSDN. 我会介绍关于Android的一些有趣的小知识点. 本文是第三篇, 其余第一篇, 第二篇. imageMogr2/auto-orient/stri ...
- U盘 文件被隐藏解决办法
原地址:http://www.deyi.com/thread-351635-1-1.html 方法:运行cmd( 在任意目录都行)单个文件 :attrib c:\"要修改的文件夹名字&quo ...
- maven插件介绍之maven-jar-plugin
maven-jar-plugin 插件的maven依赖为: <dependency> <groupId>org.apache.maven.plugins</groupId ...
- 图像处理之 opencv 学习---opencv 中的常用算法
http://blog.csdn.net/lindazhou2005/article/details/1534234 文中有提到鲁棒性 http://blog.csdn.net/chary8088/a ...
- region split流程分析
region split流程分析 splitregion的发起主要通过client端调用regionserver.splitRegion或memstore.flsuh时检查并发起. Client通过r ...
- Java网络编程知识点(1)
怎样将一个String对象转换成InputStream对象? ByteArrayInputStream inputStream = new ByteArrayInputStream(str.getBy ...
- 【ACdream】1157 Segments cdq分治
Segments Problem Description 由3钟类型操作:1)D L R(1 <= L <= R <= 1000000000) 增加一条线段[L,R]2)C i ...
- 如何查看Java进程并获取进程ID?
1. 在 LINUX 命令平台输入 1-2 个字符后按 Tab 键会自动补全后面的部分(前提是要有这个东西,例如在装了 tomcat 的前提下, 输入 tomcat 的 to 按 tab).2. ps ...
- 您可能试图从server上的安全浏览器訪问此站点。请启用脚本然后又一次载入此页。
您可能试图从server上的安全浏览器訪问此站点.请启用脚本然后又一次载入此页. 我使用域Admin组的账户登入SharePoint2010的server,打开SharePoint首页 ...
- linux杀死进程
http://blog.csdn.net/andy572633/article/details/7211546 ps -ef | grep firefox kill -s 9 1827