POJ3415:Common Substrings——题解
http://poj.org/problem?id=3415
给定两个字符串A 和B,求长度不小于k 的公共子串的个数(可以相同)。
论文题,和上道题(POJ2774)类似,首先想到现将AB串合并,然后子串可以表示成字符串后缀的前缀,于是我们比较任意两个A后缀和B后缀,用height求出他们的公共子串长度就很好做了。
(不懂为什么好做的可以看SPOJ694)
那么最坏的想法就是每遇到B后缀就和前面遇到的A后缀比较,这样显然TLE,也没用到height数组的优越性。
但是我们A后缀可以用单调栈维护,这样复杂度即可。
(大致细节:对照代码,tot是暂时存答案的地方,当遇到height数组比单调栈顶小时开始弹出,同时高出height的部分要从tot被扣掉(注意要扣掉(二者之间的后缀数)次,因为他们都重复计算了),只有当A前缀出现的时候才能用tot更新ans)
同理再对A后缀做一遍如上操作,两次答案之和即为最终所求。
(感觉是懂了,还是迷迷糊糊的,可以看这个:https://www.cnblogs.com/CSU3901130321/p/4516109.html)
(其实最关键的是两个后缀的公共前缀是他们排名之间所有的height的最小值,所以一遇到变低的height就需要更新了,并且说明之前的所有都被多算了。)
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cctype>
#include<cstdio>
#include<vector>
#include<queue>
#include<cmath>
using namespace std;
typedef long long ll;
const int N=2e5+;
char s[N];
int n,m,len,rank[N],sa[N],height[N],w[N];
inline bool pan(int *x,int i,int j,int k){
int ti=i+k<n?x[i+k]:-;
int tj=j+k<n?x[j+k]:-;
return x[i]==x[j]&&ti==tj;
}
inline void SA_init(){
int *x=rank,*y=height,r=;
for(int i=;i<r;i++)w[i]=;
for(int i=;i<n;i++)w[s[i]]++;
for(int i=;i<r;i++)w[i]+=w[i-];
for(int i=n-;i>=;i--)sa[--w[s[i]]]=i;
r=;x[sa[]]=;
for(int i=;i<n;i++)
x[sa[i]]=s[sa[i]]==s[sa[i-]]?r-:r++;
for(int k=;r<n;k<<=){
int yn=;
for(int i=n-k;i<n;i++)y[yn++]=i;
for(int i=;i<n;i++)
if(sa[i]>=k)y[yn++]=sa[i]-k;
for(int i=;i<r;i++)w[i]=;
for(int i=;i<n;i++)++w[x[y[i]]];
for(int i=;i<r;i++)w[i]+=w[i-];
for(int i=n-;i>=;i--)sa[--w[x[y[i]]]]=y[i];
swap(x,y);r=;x[sa[]]=;
for(int i=;i<n;i++)
x[sa[i]]=pan(y,sa[i],sa[i-],k)?r-:r++;
}
for(int i=;i<n;i++)rank[i]=x[i];
}
inline void height_init(){
int i,j,k=;
for(i=;i<=n;i++)rank[sa[i]]=i;
for(i=;i<n;i++){
if(k)k--;
else k=;
j=sa[rank[i]-];
while(s[i+k]==s[j+k])k++;
height[rank[i]]=k;
}
}
ll ans,tot;
int q[N][],top;
ll solve(){
ans=tot=top=;
for(int i=;i<=n;i++){
if(height[i]<m)top=tot=;
else{
int cnt=;
if(sa[i-]<len)cnt++,tot+=height[i]-m+;
while(top>&&height[i]<=q[top-][]){
top--;
tot-=q[top][]*(q[top][]-height[i]);
cnt+=q[top][];
}
q[top][]=height[i];q[top++][]=cnt;
if(sa[i]>len)ans+=tot;
}
}
tot=top=;
for(int i=;i<=n;i++){
if(height[i]<m)top=tot=;
else{
int cnt=;
if(sa[i-]>len)cnt++,tot+=height[i]-m+;
while(top>&&height[i]<=q[top-][]){
top--;
tot-=q[top][]*(q[top][]-height[i]);
cnt+=q[top][];
}
q[top][]=height[i];q[top++][]=cnt;
if(sa[i]<len)ans+=tot;
}
}
return ans;
}
int main(){
while(scanf("%d",&m)!=EOF&&m){
scanf("%s",s);
len=n=strlen(s);
s[n++]=;
scanf("%s",s+n);
n=strlen(s);
s[n++]=;
SA_init();
n--;
height_init();
printf("%lld\n",solve());
}
return ;
}
+++++++++++++++++++++++++++++++++++++++++++
+本文作者:luyouqi233。 +
+欢迎访问我的博客:http://www.cnblogs.com/luyouqi233/+
+++++++++++++++++++++++++++++++++++++++++++
POJ3415:Common Substrings——题解的更多相关文章
- POJ3415 Common Substrings —— 后缀数组 + 单调栈 公共子串个数
题目链接:https://vjudge.net/problem/POJ-3415 Common Substrings Time Limit: 5000MS Memory Limit: 65536K ...
- POJ3415 Common Substrings(后缀数组 单调栈)
借用罗穗骞论文中的讲解: 计算A 的所有后缀和B 的所有后缀之间的最长公共前缀的长度,把最长公共前缀长度不小于k 的部分全部加起来.先将两个字符串连起来,中间用一个没有出现过的字符隔开.按height ...
- poj3415 Common Substrings(后缀数组,单调栈 | 后缀自动机)
[题目链接] http://poj.org/problem?id=3415 [题意] A与B长度至少为k的公共子串个数. [思路] 基本思想是将AB各个后缀的lcp-k+1的值求和.首先将两个字符串拼 ...
- POJ3415 Common Substrings
后缀数组 求长度不小于k的公共子串的个数 代码: #include <stdio.h> #include <string.h> ; int len, len1; int wa[ ...
- 2018.12.15 poj3415 Common Substrings(后缀自动机)
传送门 后缀自动机基础题. 给两个字符串,让你求长度不小于kkk的公共子串的数量. 这题可以用后缀自动机解决废话 考虑对其中一个字串建出后缀自动机,然后用另一个在上面跑,注意到如果一个状态有贡献的话, ...
- POJ3415 Common Substrings 【后缀数组 + 单调栈】
常见的子串 时间限制: 5000MS 内存限制: 65536K 提交总数: 11942 接受: 4051 描述 字符串T的子字符串被定义为: Ť(我,ķ)= Ť 我 Ť 我 1 ... Ť I ...
- poj3415 Common Substrings (后缀数组+单调队列)
Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 9414 Accepted: 3123 Description A sub ...
- 【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≤ ...
- POJ 3415 Common Substrings(后缀数组 + 单调栈)题解
题意: 给两个串\(A.B\),问你长度\(>=k\)的有几对公共子串 思路: 先想一个朴素算法: 把\(B\)接在\(A\)后面,然后去跑后缀数组,得到\(height\)数组,那么直接\(r ...
随机推荐
- Linux命令应用大词典-第28章 硬件管理
28.1 lscpu:显示有关CPU架构的信息 28.2 nproc:显示当前进程可用的CPU数目 28.3 chcpu:配置CPU
- 第六章 高级I/O函数
第六章 高级I/O函数 6.1 pipe函数 即管道函数,用于进程间的通信. #include<unistd.h> int pipe(int fd[2]); // fd:filedes / ...
- (C#)原型模式—深复制与浅复制
1.原型模式 用原型实例指定创建对象的实例,并且通过拷贝这些原型创建新的对象. *原型模式隐藏了创建对象的细节,提高了性能. *浅复制:被复制对象的所有变量都含有与原来对象相同的值,而且所有对其他对象 ...
- 175. Invert Binary Tree【LintCode by java】
Description Invert a binary tree. Example 1 1 / \ / \ 2 3 => 3 2 / \ 4 4 解题:题目要求讲二叉树的左子树和右子树对调 ...
- 167. Add Two Numbers【LintCode by java】
Description You have two numbers represented by a linked list, where each node contains a single dig ...
- UVa 1225 - Digit Counting - ACM/ICPC Danang 2007 解题报告 - C语言
1.题目大意 把前n$(n\le 10000)$个整数顺次写在一起:12345678910111213……计算0~9各出现了多少次. 2.思路 第一想法是打表,然而觉得稍微有点暴力.不过暂时没有想到更 ...
- lvs+keepalived详解
常用软件安装及使用目录 资源链接:https://pan.baidu.com/s/15rFjO-EnTOyiTM7YRkbxuA 网盘分享的文件在此 官网:http://www.linuxvir ...
- windows下cudnn的安装过程
在CUDA安装成功之后,系统环境变量中会有如下两个变量显示:CUDA_PATH和CUDA_PATH_8 在安装完CUDA之后,到官网下载与其版本对应的CUDNN 下载地址:https:/ ...
- 在mysql启用远程连接
1.在ubuntu下面安装mysql. apt-get install mysql-server mysql-client -y 2.修改/etc/mysql/my.cnf文件. #bind-addr ...
- 网众远程修改ip、dns
修改文件 修改ip vi /etc/rc.d/rc.inetd1.config IPADDR[0] 对应第一块网卡的ip 修改dns vi /etc/resolv.conf nameserver 21 ...