借用罗穗骞论文中的讲解:

计算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(后缀数组 单调栈)的更多相关文章

  1. POJ3415 Common Substrings —— 后缀数组 + 单调栈 公共子串个数

    题目链接:https://vjudge.net/problem/POJ-3415 Common Substrings Time Limit: 5000MS   Memory Limit: 65536K ...

  2. poj 3415 Common Substrings 后缀数组+单调栈

    题目链接 题意:求解两个字符串长度 大于等于k的所有相同子串对有多少个,子串可以相同,只要位置不同即可:两个字符串的长度不超过1e5; 如 s1 = "xx" 和 s2 = &qu ...

  3. poj 3415 Common Substrings——后缀数组+单调栈

    题目:http://poj.org/problem?id=3415 因为求 LCP 是后缀数组的 ht[ ] 上的一段取 min ,所以考虑算出 ht[ ] 之后枚举每个位置作为右端的贡献. 一开始想 ...

  4. poj 3415 Common Substrings —— 后缀数组+单调栈

    题目:http://poj.org/problem?id=3415 先用后缀数组处理出 ht[i]: 用单调栈维护当前位置 ht[i] 对之前的 ht[j] 取 min 的结果,也就是当前的后缀与之前 ...

  5. poj3415 Common Substrings (后缀数组+单调队列)

    Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 9414   Accepted: 3123 Description A sub ...

  6. 【BZOJ-3238】差异 后缀数组 + 单调栈

    3238: [Ahoi2013]差异 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 1561  Solved: 734[Submit][Status] ...

  7. BZOJ_3879_SvT_后缀数组+单调栈

    BZOJ_3879_SvT_后缀数组+单调栈 Description (我并不想告诉你题目名字是什么鬼) 有一个长度为n的仅包含小写字母的字符串S,下标范围为[1,n]. 现在有若干组询问,对于每一个 ...

  8. BZOJ_3238_[Ahoi2013]差异_后缀数组+单调栈

    BZOJ_3238_[Ahoi2013]差异_后缀数组+单调栈 Description Input 一行,一个字符串S Output 一行,一个整数,表示所求值 Sample Input cacao ...

  9. BZOJ.4199.[NOI2015]品酒大会(后缀数组 单调栈)

    BZOJ 洛谷 后缀自动机做法. 洛谷上SAM比SA慢...BZOJ SAM却能快近一倍... 显然只需要考虑极长的相同子串的贡献,然后求后缀和/后缀\(\max\)就可以了. 对于相同子串,我们能想 ...

随机推荐

  1. Springmvc常用注解

    1. @RequestMapping注解的作用位置 @RequestMapping可以作用在类名上,也可以作用在方法上. 如果都有, 产生作用的路径是类名上的路径+方法上的路径. 比如Employee ...

  2. javascript 常用技巧

    1. 将彻底屏蔽鼠标右键 oncontextmenu=”window.event.returnValue=false” < table border oncontextmenu=return(f ...

  3. spring mvc 避免IE执行AJAX时,返回JSON出现下载文件

    <!-- 避免IE执行AJAX时,返回JSON出现下载文件 --> <bean id="mappingJacksonHttpMessageConverter" c ...

  4. struts2 学习路线

    1.请求:开发struts2流程,(常见配置.action的方法的访问.ServletApi的访问.结果页面类型) 2.强求:封装参数.类型转换.数据校验.国际化.拦截器. 3.响应页面的生成:文件上 ...

  5. jenkins结合ansible用shell实现自动化部署和回滚

    最近用jenkins+gitlab+ansible做持续化集成,自动化部署和版本回滚.然而deploy plugin没能做到增量升级和回滚操作,折腾了很久决定自己写个脚本来简单实现. 环境: cent ...

  6. Unity3D 降低IL2CPP编译可执行文件大小

    项目开始使用IL2CPP编译,后果是可执行文件急剧增加. google后发现国外一大神写的方法,原帖在这http://forum.unity3d.com/threads/suggestion-for- ...

  7. ACM/ICPC 之 拓扑排序+DFS(POJ1128(ZOJ1083)-POJ1270)

    两道经典的同类型拓扑排序+DFS问题,第二题较第一题简单,其中的难点在于字典序输出+建立单向无环图,另外理解题意是最难的难点,没有之一... POJ1128(ZOJ1083)-Frame Stacki ...

  8. Mac会给你一些欣喜

    Mac会给你一些欣喜 以前一直没有用过Mac,一直都是用Windows的电脑,只是偶尔会去用Ubuntu这样的Linux系统.Mac OS 确实是一只可以给你欣喜的系统. 上周拿到公司分发的Mac,到 ...

  9. codeforces 490C. Hacking Cypher 解题报告

    题目链接:http://codeforces.com/problemset/problem/490/C 题目意思:给出一个可能有10^6 位长的字符串且没有前导0的整数,问能否一分为二,使得前面的一部 ...

  10. C#关于new的用法

    1.运算符就是在实例化一个类的时候(运算符的用法) A a=new A(); 2.new 约束指定泛型类声明中的任何类型参数都必须有公共无参数构造函数.当泛型类创建类型的新实例时,将此约束应用于类型参 ...