http://poj.org/problem?id=3415 (题目链接)

题意

  给定两个字符串 A 和 B,求长度不小于 k 的公共子串的个数(可以相同)。

Solution

  后缀数组论文题。。。

  基本思路是计算 A 的所有后缀和 B 的所有后缀之间的最长公共前缀的长度,把最长公共前缀长度不小于 k 的部分全部加起来。先将两个字符串连起来,中间用一个没有出现过的字符隔开。按 height 值分组后,接下来的工作便是快速的统计每组中后缀之间的最长公共前缀之和。扫描一遍,每遇到一个 B 的后缀就统计与前面的 A 的后缀能产生多少个长度不小于 k 的公共子串,这里 A 的后缀需要用一个单调的栈来高效的维护。然后对 A 也这样做一次。

  如何用单调栈来维护呢?这真的是一个问题。这里我运用的单调栈与一般的单调栈不一样。单调栈里面记录一个结构体,结构体记录每个串对答案的贡献w以及这种串的个数c,自栈底向栈顶w递增。每次扫描到一个height[i]当它小于栈顶时,将栈顶的元素与栈顶第二个元素合并,并且更新栈中元素的总贡献。

细节

  数组开两倍。

代码

// poj3693
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#define LL long long
#define inf 1<<30
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std; const int maxn=500010;
int sa[maxn],rank[maxn],height[maxn];
int n,K;
char s[maxn]; struct data {int w,c;}st[maxn];
namespace Suffix {
int wa[maxn],wb[maxn],ww[maxn];
bool cmp(int *r,int a,int b,int l) {
return r[a]==r[b] && r[a+l]==r[b+l];
}
void da(char *r,int *sa,int n,int m) {
int i,j,p,*x=wa,*y=wb;
for (i=0;i<=m;i++) ww[i]=0;
for (i=1;i<=n;i++) ww[x[i]=r[i]]++;
for (i=1;i<=m;i++) ww[i]+=ww[i-1];
for (i=n;i>=1;i--) sa[ww[x[i]]--]=i;
for (p=0,j=1;p<n;j*=2,m=p) {
for (p=0,i=n-j+1;i<=n;i++) y[++p]=i;
for (i=1;i<=n;i++) if (sa[i]>j) y[++p]=sa[i]-j;
for (i=0;i<=m;i++) ww[i]=0;
for (i=1;i<=n;i++) ww[x[y[i]]]++;
for (i=1;i<=m;i++) ww[i]+=ww[i-1];
for (i=n;i>=1;i--) sa[ww[x[y[i]]]--]=y[i];
for (swap(x,y),p=x[sa[1]]=1,i=2;i<=n;i++)
x[sa[i]]=cmp(y,sa[i-1],sa[i],j) ? p : ++p;
}
}
void calheight(char *r,int *sa,int n) {
for (int i=1;i<=n;i++) rank[sa[i]]=i;
for (int k=0,i=1;i<=n;i++) {
if (k) k--;
int j=sa[rank[i]-1];
while (r[i+k]==r[j+k]) k++;
height[rank[i]]=k;
}
}
} int main() {
while (scanf("%d",&K)!=EOF && K) {
scanf("%s",s+1);
int n=strlen(s+1);
s[++n]='#';
int l=n;
scanf("%s",s+n+1);
n=strlen(s+1);
Suffix::da(s,sa,n,300);
Suffix::calheight(s,sa,n);
int top=0;LL ans=0,S=0;
height[n+1]=inf;
for (int i=1;i<=n+1;i++) {
if (sa[i]>l && i!=n+1) ans+=S;
if (height[i+1]>=K) {
while (top>1 && st[top-1].w>height[i+1]-K+1) {
st[top-1].c+=st[top].c;
S-=(st[top].w-st[top-1].w)*st[top].c;
st[top--]=(data){0,0};
}
if (st[top].w>height[i+1]-K+1) {
if (st[top-1].w==height[i+1]-K+1) {
st[top-1].c+=st[top].c;
S-=(st[top].w-st[top-1].w)*st[top].c;
st[top--]=(data){0,0};
}
else {S-=(st[top].w-(height[i+1]-K+1))*st[top].c;st[top].w=height[i+1]-K+1;}
}
if (sa[i]<l) {
if (st[top].w==height[i+1]-K+1) st[top].c++;
else st[++top]=(data){height[i+1]-K+1,1};
S+=height[i+1]-K+1;
}
}
else {while (top) st[top--]=(data){0,0};S=0;}
}
for (int i=1;i<=n+1;i++) {
if (sa[i]<l && i!=n+1) ans+=S;
if (height[i+1]>=K) {
while (top>1 && st[top-1].w>height[i+1]-K+1) {
st[top-1].c+=st[top].c;
S-=(st[top].w-st[top-1].w)*st[top].c;
st[top--]=(data){0,0};
}
if (st[top].w>height[i+1]-K+1) {
if (st[top-1].w==height[i+1]-K+1) {
st[top-1].c+=st[top].c;
S-=(st[top].w-st[top-1].w)*st[top].c;
st[top--]=(data){0,0};
}
else {S-=(st[top].w-(height[i+1]-K+1))*st[top].c;st[top].w=height[i+1]-K+1;}
}
if (sa[i]>l) {
if (st[top].w==height[i+1]-K+1) st[top].c++;
else st[++top]=(data){height[i+1]-K+1,1};
S+=height[i+1]-K+1;
}
}
else {while (top) st[top--]=(data){0,0};S=0;}
}
printf("%lld\n",ans);
}
return 0;
}

【poj3415】 Common Substrings的更多相关文章

  1. 【POJ3415】 Common Substrings(后缀数组|SAM)

    Common Substrings Description A substring of a string T is defined as: T(i, k)=TiTi+1...Ti+k-1, 1≤i≤ ...

  2. 【POJ3415】Common Substrings(后缀数组,单调栈)

    题意: n<=1e5 思路: 我的做法和题解有些不同 题解是维护A的单调栈算B的贡献,反过来再做一次 我是去掉起始位置不同这个限制条件先算总方案数,再把两个串内部不合法的方案数减去 式子展开之后 ...

  3. 【POJ3415】 Common Substrings (SA+单调栈)

    这道是求长度不小于 k 的公共子串的个数...很不幸,我又TLE了... 解法参考论文以及下面的链接 http://www.cnblogs.com/vongang/archive/2012/11/20 ...

  4. 【SPOJ】Distinct Substrings(后缀自动机)

    [SPOJ]Distinct Substrings(后缀自动机) 题面 Vjudge 题意:求一个串的不同子串的数量 题解 对于这个串构建后缀自动机之后 我们知道每个串出现的次数就是\(right/e ...

  5. 【SPOJ】Distinct Substrings/New Distinct Substrings(后缀数组)

    [SPOJ]Distinct Substrings/New Distinct Substrings(后缀数组) 题面 Vjudge1 Vjudge2 题解 要求的是串的不同的子串个数 两道一模一样的题 ...

  6. 【CF316G3】Good Substrings 后缀自动机

    [CF316G3]Good Substrings 题意:给出n个限制(p,l,r),我们称一个字符串满足一个限制当且仅当这个字符串在p中的出现次数在[l,r]之间.现在想问你S的所有本质不同的子串中, ...

  7. 【Aizu2292】Common Palindromes(回文树)

    [Aizu2292]Common Palindromes(回文树) 题面 Vjudge 神TMD日语 翻译: 给定两个字符串\(S,T\),询问\((i,j,k,l)\)这样的四元组个数 满足\(S[ ...

  8. 【SPOJ】Distinct Substrings

    [SPOJ]Distinct Substrings 求不同子串数量 统计每个点有效的字符串数量(第一次出现的) \(\sum\limits_{now=1}^{nod}now.longest-paren ...

  9. 【POJ 3415】Common Substrings

    [链接]h在这里写链接 [题意]     求两个串的长度大于等于k的公共子串个数.     相同的重复计数. [题解]     先把两个字符串用一个分隔符分开.最好比出现的字符都大的一个数字.    ...

随机推荐

  1. c# update check

    public class UpdateChecker { public static event EventHandler completeCheck; private static bool isC ...

  2. web窗体的运用

    using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace WebAp ...

  3. [Deep-Learning-with-Python]基于Kears的Reuters新闻分类

    Reuters数据集下载速度慢,可以在我的repo库中找到下载,下载后放到~/.keras/datasets/目录下,即可正常运行. 构建神经网络将路透社新闻分类,一共有46个类别.因为有多个类别,属 ...

  4. windows系统中Dotnet core runtime 安装后,无法启动次程序,因为计算机中丢失api-ms-win-crt-runtime-l1-1-0.dll的解决方法

    因为dotnet core runtime依赖vc++2015,如果系统未安装vc++2015则会报上面的错误 解决方案:先下载安装vc++2015再安装dotnet core runtime, vc ...

  5. mysql 配置 root 远程访问

    来源: https://www.cnblogs.com/24la/p/mariadb-remoting-access.html 首先配置允许访问的用户,采用授权的方式给用户权限 GRANT ALL P ...

  6. Kosaraju算法、Tarjan算法分析及证明--强连通分量的线性算法

    一.背景介绍 强连通分量是有向图中的一个子图,在该子图中,所有的节点都可以沿着某条路径访问其他节点.强连通性是一种非常重要的等价抽象,因为它满足 自反性:顶点V和它本身是强连通的 对称性:如果顶点V和 ...

  7. 前端项目模块化的实践1:搭建 NPM 私有仓库管理源码及依赖

    以下是关于前端项目模块化的实践,包含以下内容: 搭建 NPM 私有仓库管理源码及依赖: 使用 Webpack 打包基础设施代码: 使用 TypeScript 编写可靠类库 使用 TypeScript ...

  8. OpenCV操作像素

    在了解了图像的基础知识和OpenCV的基础知识和操作以后,接下来我们要做的就对像素进行操作,我们知道了图像的本质就是一个矩阵,那么一个矩阵中存储了那么多的像素,我们如何来操作呢?下面通过几个例子来看看 ...

  9. linux一切皆文件之tty字符设备(深入理解sshd创建pty的过程) (五)

    一.知识准备 1.在linux中,一切皆为文件,所有不同种类的类型都被抽象成文件(比如:块设备,socket套接字,pipe队列) 2.操作这些不同的类型就像操作文件一样,比如增删改查等 3.块设备支 ...

  10. EOS开发基础之四:使用cleos命令行客户端操作EOS——智能合约之eosio.bios和eosio.token

    现实世界中的合约,简单地说,是一个参与活动的所有人都需要遵循的协议.合约可以是正式的法律合同(例如,金融交易),或者是简单的游戏规则.典型的活动可以是诸如资金转移(在金融合约的情况下)或游戏动作(在游 ...