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 ...
随机推荐
- 注解 @AutoConfigureBefore 和 @AutoConfigureAfter 的用途
注解 @AutoConfigureBefore 和 @AutoConfigureAfter 的用途 介绍: 如果你想将在SpringBoot项目中的配置类进行排序,那么用到spring-boot-au ...
- MySQL调优之查询优化
一.查询慢的原因 1.网络 (1)网络丢包,重传 这个比较容易理解.当SQL 从客户端发送到数据库,执行完毕,数据库将结果返回给客户端,这个将数据返回给客户端的过程本质是网络包传输.因为链路的不稳定性 ...
- 找出10000内的素数 CSP
"Problem: To print in ascending order all primes less than 10000. Use an array of processes, SI ...
- Go GC: Latency Problem Solved
https://talks.golang.org/2015/go-gc.pdf https://www.oschina.net/translate/go-gc-solving-the-latency- ...
- LDAP学习
.top pre { display: block; background: rgba(68, 67, 65, 1); color: rgba(255, 255, 255, 1); margin: 1 ...
- 像羽毛一样轻的MVVMLight(一)(MVVM 和 MVVMLight简介)
致敬 在此致敬翁智华大佬,感谢大佬为后辈们写下如此详细的文档,本文将在原文基础上添加些自己的理解,希望这样优秀的文档广为流传. 原文请参考 https://www.cnblogs.com/wzh201 ...
- git本地检出远程分支
场景:本地分支被误物理删除,想要重新将自己的分支代码从远程拉取下来.(此时取的是最后一次git push上去的分支代码) 1.与远程仓库重新建立关系 1 git clone git@gitlab.名称 ...
- 十二:SpringBoot-基于Cache注解模式,管理Redis缓存
SpringBoot-基于Cache注解模式,管理Redis缓存 1.Cache缓存简介 2.核心API说明 3.SpringBoot整合Cache 3.1 核心依赖 3.2 Cache缓存配置 3. ...
- PL/SQL 学习分享(续)
事务 事务的概述 事务的特性 回滚点 事务实例练习 动态SQL 动态SQL概述 动态SQL应用场合 动态SQL的执行语法 绑定变量 动态SQL创建表 动态SQL绑定变量 动态SQL综合案例添加数据 使 ...
- 36.Samba 文件共享服务1--安装及配置参数解释
1.Samba 服务程序现在已经成为在Linux 系统与Windows系统之间共享文件的最佳选择. 1)安装: [root@localhost ~]#yum install samba Loaded ...