POJ3415 Common Substrings(后缀数组 单调栈)
借用罗穗骞论文中的讲解:
计算A 的所有后缀和B 的所有后缀之间的最长公共前缀的长度,把最长公共前缀长度不小于k 的部分全部加起来。先将两个字符串连起来,中间用一个没有出现过的字符隔开。按height 值分组后,接下来的工作便是快速的统计每组中后缀之间的最长公共前缀之和。扫描一遍,每遇到一个B 的后缀就统计与前面的A 的后缀能产生多少个长度不小于k 的公共子串,这里A 的后缀需要用一个单调的栈来高效的维护。然后对A 也这样做一次。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
using namespace std;
const int N = 210008;
typedef long long LL; int val[N], sum[N], wa[N], wb[N];
int sa[N], rk[N], height[N];
char a[N], b[N]; inline bool cmp(int str[], int a, int b, int l){
return str[a] == str[b] && str[a + l] == str[b + l];
} void da(char str[], int n, int m){ int *x = wa, *y = wb;
memset(sum, 0, sizeof(sum)); for(int i = 0; i < n; i++){
sum[x[i] = str[i]]++;
}
for(int i = 1; i < m; i++){
sum[i] += sum[i - 1];
}
for(int i = n - 1; i >= 0; i--){
sa[--sum[ x[i]] ] = i;
}
for(int j = 1, p = 1; p < n; j *= 2, m = p){
p = 0;
for(int i = n - j; i < n; i++){
y[p++] = i;
}
for(int i = 0; i < n; i++){
if(sa[i] >= j){
y[p++] = sa[i] - j;
}
}
for(int i = 0; i < n; i++){
val[i] = x[ y[i] ];
} memset(sum , 0, sizeof(sum));
for(int i = 0; i < n; i++){
sum[val[i]]++;
}
for(int i = 1; i < m; i++){
sum[i] += sum[i - 1];
}
for(int i = n - 1; i >= 0; i--){
sa[--sum[ val[i] ]] = y[i];
} swap(x, y);
x[sa[0]] = 0;
p = 1;
for(int i = 1 ; i < n; i++){
x[sa[i]] = cmp(y, sa[i - 1], sa[i], j)? p - 1:p++;
}
}
} void getHeight(char str[], int n){
for(int i = 1; i <= n; i++){
rk[ sa[i] ] = i;
}
int k = 0;
for(int i = 0; i < n; height[rk[i++]] = k){
if(k) k--;
int j = sa[rk[i] - 1];
while(str[i + k] == str[j + k]){
k++;
}
}
} struct node{
int h;
LL cnt;
}stk[N]; int main(){ int k;
while(~scanf("%d", &k) && k){
scanf("%s %s", a, b);
int n = strlen(a);
int m = strlen(b);
int len = n + m + 1;
a[n] = 125;
for(int i = n + 1, j = 0; j < m; i++, j++){
a[i] = b[j];
}
a[len] = 0;
da(a, len + 1, 150);
getHeight(a, len);
LL sum = 0;
int top = 0;
LL tot = 0;
for(int i = 1; i <= len ; i++){
int cnt = 0;
if(height[i] < k){
top = 0;
tot = 0;
}else{
if(sa[i - 1] < n){
cnt++;
tot += height[i] - k + 1;
}
while(top > 0 && stk[top - 1].h > height[i]){
top--;
cnt += stk[top].cnt;
tot -= (stk[top].h - height[i]) * stk[top].cnt;
}
stk[top].h = height[i];
stk[top].cnt = cnt;
top++;
if(sa[i] > n){
sum += tot;
}
}
} top = 0;
tot = 0;
for(int i = 1; i <= len ; i++){
int cnt = 0;
if(height[i] < k){
top = 0;
tot = 0; }else{
if(sa[i - 1] > n){
cnt++;
tot += height[i] - k + 1;
}
while(top > 0 && stk[top - 1].h > height[i]){
top--;
cnt += stk[top].cnt;
tot -= (stk[top].h - height[i]) * stk[top].cnt;
}
stk[top].h = height[i];
stk[top].cnt = cnt;
top++;
if(sa[i] < n){
sum += tot;
}
}
}
printf("%I64d\n", sum);
}
return 0;
}
POJ3415 Common Substrings(后缀数组 单调栈)的更多相关文章
- POJ3415 Common Substrings —— 后缀数组 + 单调栈 公共子串个数
题目链接:https://vjudge.net/problem/POJ-3415 Common Substrings Time Limit: 5000MS Memory Limit: 65536K ...
- poj 3415 Common Substrings 后缀数组+单调栈
题目链接 题意:求解两个字符串长度 大于等于k的所有相同子串对有多少个,子串可以相同,只要位置不同即可:两个字符串的长度不超过1e5; 如 s1 = "xx" 和 s2 = &qu ...
- poj 3415 Common Substrings——后缀数组+单调栈
题目:http://poj.org/problem?id=3415 因为求 LCP 是后缀数组的 ht[ ] 上的一段取 min ,所以考虑算出 ht[ ] 之后枚举每个位置作为右端的贡献. 一开始想 ...
- poj 3415 Common Substrings —— 后缀数组+单调栈
题目:http://poj.org/problem?id=3415 先用后缀数组处理出 ht[i]: 用单调栈维护当前位置 ht[i] 对之前的 ht[j] 取 min 的结果,也就是当前的后缀与之前 ...
- poj3415 Common Substrings (后缀数组+单调队列)
Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 9414 Accepted: 3123 Description A sub ...
- 【BZOJ-3238】差异 后缀数组 + 单调栈
3238: [Ahoi2013]差异 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 1561 Solved: 734[Submit][Status] ...
- BZOJ_3879_SvT_后缀数组+单调栈
BZOJ_3879_SvT_后缀数组+单调栈 Description (我并不想告诉你题目名字是什么鬼) 有一个长度为n的仅包含小写字母的字符串S,下标范围为[1,n]. 现在有若干组询问,对于每一个 ...
- BZOJ_3238_[Ahoi2013]差异_后缀数组+单调栈
BZOJ_3238_[Ahoi2013]差异_后缀数组+单调栈 Description Input 一行,一个字符串S Output 一行,一个整数,表示所求值 Sample Input cacao ...
- BZOJ.4199.[NOI2015]品酒大会(后缀数组 单调栈)
BZOJ 洛谷 后缀自动机做法. 洛谷上SAM比SA慢...BZOJ SAM却能快近一倍... 显然只需要考虑极长的相同子串的贡献,然后求后缀和/后缀\(\max\)就可以了. 对于相同子串,我们能想 ...
随机推荐
- linux操作系统flash player问题--ubuntu
adobe公司停止了对linux系统的flash player的更新,这导致很多网页视频不能够通过浏览器观看,很是不爽! 还好,给用户留下了一点点希望,那便是chrome浏览器. 谷歌浏览器,有一款插 ...
- 保存字符串到手机SDcard为txt文件
try { if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { File sdCardDir ...
- 算法手记 之 数据结构(并查集详解)(POJ1703)
<ACM/ICPC算法训练教程>读书笔记-这一次补上并查集的部分.将对并查集的思想进行详细阐述,并附上本人AC掉POJ1703的Code. 在一些有N个元素的集合应用问题中,通常会将每个元 ...
- js闭包问题
function picLinkInit(parentClassName, imgW, imgH, childClassObjs) { var $match = $(parentClassName); ...
- Greedy:Paint Color(AOJ 0531)
涂颜料 题目大意:在一个1000000*1000000的矩阵中放入几块木板,问你这些木板把矩阵划分成了几个区域?输入会给左下角和右上角的坐标,输入W==0且H==0结束. 这一题是书上的作业题,书上有 ...
- js验证手机号
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- mybatis 获取自增ID
在Mybatis Mapper文件中添加属性“useGeneratedKeys”和“keyProperty”,其中keyProperty是Java对象的属性名,而不是表格的字段名. <inser ...
- iOS 发送请求时获取cookie
Cookie: 记录者用户信息的保存在本地的用户数据,如果有会被自动附上 值得一提的是,在iOS中当你发送一个任意请求时,不管你愿不愿意,NSURLRequest都会自动帮你记录你所访问的URL上设置 ...
- MyEclipse在线安装maven插件最新地址
http://repository.sonatype.org/content/sites/forge-sites/m2e/0.10.0/S/20100209-0800/
- 如何让Table中的第一列和第二列的值相乘然后赋值给第三列
因为需求的原因所以这样做,不废话了,直接上代码,我用的GridView绑定的数据,table也一样,因为GridView通过浏览器编译后的代码就是table.下面是aspx页面的Html代码: < ...