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. Java基础—线程

    推荐阅读:http://www.iteye.com/topic/806990 一.起手式——基本概念 1.什么叫线程 进程:进行中的程序:作为资源分配的单位. 线程:轻量级的进程:程序里的顺序控制流, ...

  2. Elasticsearch Java Rest Client API 整理总结 (一)——Document API

    目录 引言 概述 High REST Client 起步 兼容性 Java Doc 地址 Maven 配置 依赖 初始化 文档 API Index API GET API Exists API Del ...

  3. How to export data from Thermo-Calc 如何从Thermo-calc导出文本数据

    记录20180510 问题:如何从thermo-calc导出文本数据供origin绘图? 解决: In Thermo-Calc graphical mode, you can just add a ' ...

  4. DFA化简

    首先是未化简DFA的转换表 NFA状态 DFA状态 a b {0,1,2,4,7} A B C {1,2,3,4,6,7,8} B B D {1,2,4,5,6,7} C B C {1,2,4,5,6 ...

  5. [转]申瓯 JSY2000-06 程控电话交换机呼叫转移设置

    说明:若申瓯程控电话交换机分机有事不在位置上或遇忙分机正忙时为使某些重要来话不丢失,可设置将呼入本机的电话转移至其他分机及公网固定电话或手机.电话交换机使用了本功能不管分机用户在什么地方都能接听到办公 ...

  6. DevOps架构下如何进行微服务性能测试?

    一. 微服务架构下的性能测试挑战 微服务与DevOps 微服务是实现DevOps的重要架构 微服务3S原则 DevOps核心点 微服务架构下的业务特点 亿级用户的平台 单服务业务随时扩容 服务之间存在 ...

  7. 机器学习初入门02 - Pandas的基本操作

    之前的numpy可以说是一个针对矩阵运算的库,这个Pandas可以说是一个实现数据处理的库,Pandas底层的许多函数正是基于numpy实现的 一.Pandas数据读取 1.pandas.read_c ...

  8. metasploit学习之情报搜集

    3.1.被动信息搜集whois查询Netcraft nslookup>set type=mx>testfire.net Google Hacking 3.2 主动信息搜集 使用nmap进行 ...

  9. sqlserver批量删除字段 msrepl_tran_version

    屁话不多说. 原因: msrepl_tran_version由于有非空约束.所以不能直接删除. --###############################################--1 ...

  10. CentOS 7 Apache服务的安装与配置

    原文出处:http://blog.51cto.com/13525470/2070375 一.Apache简介 Apache 是一个知名的开源Web服务器.早期的Apache服务器由Apache Gro ...