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 ...
随机推荐
- autocomplete.jquery 点击或进入默认显示所有结果
注意使用的是autocomplete.jquery,官网地址是:https://github.com/devbridge/jQuery-Autocomplete.而不是JqueryUI的autocom ...
- 获取项目中.txt 文件的内容
package com.fh.controller.ruitai.util; import java.io.BufferedInputStream; import java.io.File; impo ...
- 「日常训练」湫湫系列故事——设计风景线(HDU-4514)
题意与分析 中文题目,木得题意的讲解谢谢. 然后还是分解成两个任务:a)判环,b)找最长边. 对于这样一个无向图,强行转换成负权然后bellman-ford算法求最短是难以实现的,所以感谢没有环--我 ...
- Windows10安装GPU版本的Tensorflow
本人电脑配置(公司的)gtx1080ti,下载的的cuda8.0,cudnn6.0,python3.5.3安装完成后,安装tensorflow 1.pip install tensorflow-gpu ...
- 数据库Mysql的学习(七)-自定义函数和流程控制
DELIMITER // (设置结束符 其实我也不太明白为啥要这样 记住就行把) CREATE FUNCTION ym_date(mydate DATE) (创建函数 函数名字(参数)) ) (指定函 ...
- 春招实习汇总(7个offer)
转载出处 刚从北京到家,总算也可以歇歇了,最近一段时间真是忙于奔命的感觉,也确实体会到了找工作的艰辛,总而言之,求职之路,如人饮水,冷暖自知. 我想把这段时间找工作的体验和经历分享出来告诉大家,让大避 ...
- return语句的用法
1.return语句的作用:a.返回一个值,这个值可以是任意类型.b.使程序返回到操作系统(即终止程序)2.java中对于一个函数,不论有没有返回值类型,都可以带有return 语句.但是区别在于,r ...
- Java容器之List接口
List 接口: 1. List 接口是 Collection 的子接口,实现 List 接口的容器类中的元素是有顺序的,而且可以重复: 2. List 容器中的元素都对应一个整数型的序号记载其在容器 ...
- PCA算法理解及代码实现
github:PCA代码实现.PCA应用 本文算法均使用python3实现 1. 数据降维 在实际生产生活中,我们所获得的数据集在特征上往往具有很高的维度,对高维度的数据进行处理时消耗的时间很大, ...
- Deeplearning——Logistics回归
资料来源:1.博客:http://binweber.top/2017/09/12/deep_learning_1/#more——转载,修改更新 2.文章:https://www.qcloud.com/ ...