POJ 3415 Common Substrings(后缀数组 + 单调栈)题解
题意:
给两个串\(A、B\),问你长度\(>=k\)的有几对公共子串
思路:
先想一个朴素算法:
把\(B\)接在\(A\)后面,然后去跑后缀数组,得到\(height\)数组,那么直接\(rmq\)就能\(O(1)\)得到任意两个\(A\)和\(B\)的LCP。如果\(LCP >= k\),那么这个串的贡献对数为\(LCP - k + 1\)。但是这样遍历显然超时。
那么我们可以用单调栈优化这个问题:
我们构建一个递增的单调栈,那么栈顶就是最大,每个栈里的元素为贡献值\(height\)的大小,那么显然,某一位置对后面的贡献为\(min(height[i])\),所以如果遇到入栈元素比栈顶大,说明栈顶的贡献值从此刻开始就要变小了,那么就去更新这个贡献。
参考:
代码:
#include<map>
#include<set>
#include<queue>
#include<cmath>
#include<stack>
#include<ctime>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 2e5 + 10;
const int INF = 0x3f3f3f3f;
const ull seed = 11;
const int MOD = 1e9 + 7;
using namespace std;
int str[maxn];
int t1[maxn], t2[maxn], c[maxn];
int sa[maxn];
int rk[maxn];
int height[maxn];
bool cmp(int *r, int a, int b, int l){
return r[a] == r[b] && r[a + l] == r[b + l];
}
void da(int *str, int n, int m){
n++;
int i, j, p, *x = t1, *y = t2;
for(i = 0; i < m; i++) c[i] = 0;
for(i = 0; i < n; i++) c[x[i] = str[i]]++;
for(i = 1; i < m; i++) c[i] += c[i - 1];
for(i = n - 1; i >= 0; i--) sa[--c[x[i]]] = i;
for(j = 1; j <= n; j <<= 1){
p = 0;
for(i = n - j; i < n; i++) y[p++] = i;
for(i = 0; i < n; i++) if(sa[i] >= j) y[p++] = sa[i] - j;
for(i = 0; i < m; i++) c[i] = 0;
for(i = 0; i < n; i++) c[x[y[i]]]++;
for(i = 1; i < m; i++) c[i] += c[i - 1];
for(i = n - 1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i];
swap(x, y);
p = 1; x[sa[0]] = 0;
for(i = 1; i < n; i++)
x[sa[i]] = cmp(y, sa[i - 1], sa[i], j)? p - 1 : p++;
if(p >= n) break;
m = p;
}
int k = 0;
n--;
for(i = 0; i <= n; i++) rk[sa[i]] = i;
for(i = 0; i < n; i++){
if(k) k--;
j = sa[rk[i] - 1];
while(str[i + k] == str[j + k]) k++;
height[rk[i]] = k;
}
}
char s[maxn];
ll instack[maxn], num[maxn];
int main(){
int k;
while(~scanf("%d", &k) && k){
scanf("%s", s);
int len = strlen(s);
int cnt = 0;
for(int i = 0; i < len; i++){
str[cnt++] = s[i];
}
int pos = cnt; //#
str[cnt++] = '$';
scanf("%s", s);
len = strlen(s);
for(int i = 0; i < len; i++){
str[cnt++] = s[i];
}
str[cnt] = 0;
da(str, cnt, 130);
int top = 0;
ll tot = 0, ans = 0;
for(int i = 2; i <= cnt; i++){ //栈里塞A
if(height[i] < k){
top = 0, tot = 0;
}
else{
int number = 0;
if(sa[i - 1] < pos){ //A对后面B的贡献
tot += height[i] - k + 1;
number++;
}
while(top > 0 && instack[top] >= height[i]){
tot -= num[top] * (instack[top] - k + 1);
tot += num[top] * (height[i] - k + 1);
number += num[top];
--top;
}
instack[++top] = height[i];
num[top] = number;
if(sa[i] > pos) ans += tot;
}
}
top = 0, tot = 0;
for(int i = 2; i <= cnt; i++){ //栈里塞B
if(height[i] < k){
top = 0, tot = 0;
}
else{
int number = 0;
if(sa[i - 1] > pos){ //B对后面A的贡献
tot += height[i] - k + 1;
number++;
}
while(top > 0 && instack[top] >= height[i]){
tot -= num[top] * (instack[top] - k + 1);
tot += num[top] * (height[i] - k + 1);
number += num[top];
--top;
}
instack[++top] = height[i];
num[top] = number;
if(sa[i] < pos) ans += tot;
}
}
printf("%lld\n", ans);
}
return 0;
}
POJ 3415 Common Substrings(后缀数组 + 单调栈)题解的更多相关文章
- poj 3415 Common Substrings —— 后缀数组+单调栈
题目:http://poj.org/problem?id=3415 先用后缀数组处理出 ht[i]: 用单调栈维护当前位置 ht[i] 对之前的 ht[j] 取 min 的结果,也就是当前的后缀与之前 ...
- poj 3415 Common Substrings——后缀数组+单调栈
题目:http://poj.org/problem?id=3415 因为求 LCP 是后缀数组的 ht[ ] 上的一段取 min ,所以考虑算出 ht[ ] 之后枚举每个位置作为右端的贡献. 一开始想 ...
- poj 3415 Common Substrings 后缀数组+单调栈
题目链接 题意:求解两个字符串长度 大于等于k的所有相同子串对有多少个,子串可以相同,只要位置不同即可:两个字符串的长度不超过1e5; 如 s1 = "xx" 和 s2 = &qu ...
- poj 3415 Common Substrings - 后缀数组 - 二分答案 - 单调栈
题目传送门 传送点I 传送点II 题目大意 给定串$A, B$,求$A$和$B$长度大于等于$k$的公共子串的数量. 根据常用套路,用一个奇怪的字符把$A$,$B$连接起来,然后二分答案,然后按mid ...
- 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 ...
- POJ3415 Common Substrings —— 后缀数组 + 单调栈 公共子串个数
题目链接:https://vjudge.net/problem/POJ-3415 Common Substrings Time Limit: 5000MS Memory Limit: 65536K ...
- POJ 3415 Common Substrings 后缀数组+并查集
后缀数组,看到网上很多题解都是单调栈,这里提供一个不是单调栈的做法, 首先将两个串 连接起来求height 求完之后按height值从大往小合并. height值代表的是 sa[i]和sa[i ...
- POJ - 3415 Common Substrings (后缀数组)
A substring of a string T is defined as: T( i, k)= TiTi +1... Ti+k -1, 1≤ i≤ i+k-1≤| T|. Given two s ...
- poj 3415 Common Substrings【SA+单调栈】
把两个串中间加一个未出现字符接起来,然后求SA 然后把贡献统计分为两部分,在排序后的后缀里,属于串2的后缀和排在他前面属于串1的后缀的贡献和属于串1的后缀和排在他前面属于串2的后缀的贡献 两部分分别作 ...
- POJ 3415 Common Substrings ——后缀数组
[题目分析] 判断有多少个长度不小于k的相同子串的数目. N^2显然是可以做到的. 其实可以维护一个关于height的单调栈,统计一下贡献,就可以了. 其实还是挺难写的OTZ. [代码] #inclu ...
随机推荐
- GlusterFS数据存储脑裂修复方案最全解析
本文档介绍了glusterfs中可用于监视复制卷状态的heal info命令以及解决脑裂的方法 一. 概念解析 常见术语 名称 解释 Brick GlusterFS 的基本存储单元,由可信存储池中服务 ...
- selenium浏览器弹出框alert 操作
1.简介 在WebDriver中要处理JS生成的alert.confirm以及prompt,需要 switch_to.alert() 来选取(定位)警告弹窗,在对弹窗进行关闭.输入等信息操作. 2.操 ...
- jmeter的线程数,并发用户数,TPS,RPS 关系解说
背景 在做性能测试的时候,传统方式都是用并发虚拟用户数来衡量系统的性能(站在客户端视角),一般适用于一些网页站点例如首页.H5的压测:而RPS(Requests per second)模式主要是为了方 ...
- Property attribute.
class property(object): """ Property attribute. fget function to be used for getting ...
- 三次握手 四次握手 原因分析 TCP 半连接队列 全连接队列
小结 1. 三次握手的原因:保证双方收和发消息功能正常: [生活模型] "请问能听见吗""我能听见你的声音,你能听见我的声音吗" [原理]A先对B:你在么?我在 ...
- 内联扩展 inline expansion An Inline Function is As Fast As a Macro 与宏的比较
让编译器直接将完整的函数体插入到每一个调用该函数的地方,从而提高函数调用的运行速度. 优秀的JIT编译器会通过侦测运行信息,仅将需要频繁运行的瓶颈部分进行编译,从而大大削减编译所需的时间. 而且,利用 ...
- vscode远程开发安装
https://www.cnblogs.com/xiaoqi/p/vs-code-remote.html ============================= https://blog.csdn ...
- 2021最新WordPress安装教程(一):Centos7安装Apache
一转眼2020年已经过去了,看网络上很多WordPress的安装教程都比较旧,有些写的不太详细,WordPress是站长最喜欢的一款建站系统,数据统计到2020年为止,WordPress在所有网站的市 ...
- OSPF优化
1.点对点(背靠背)的优化 两台设备直连(逻辑上的直连). 将OSPF宣告接口配置为点对点模式,不用选举DR.减少20S时间 interface Ethernet0/1 ip ospf network ...
- 利用ELK构建一个小型的日志收集平台
利用ELK构建一个小型日志收集平台 伴随着应用以及集群的扩展,查看日志的方式总是不方便,我们希望可以有一个便于我们查询及提醒功能的平台:那么首先需要剖析有几步呢? 格式定义 --> 日志收集 - ...