@description@

给定两个长度为 n 的字符串 a, b 与一个长度为 m 的字符串 s。

问存在多少对区间 [l1, r1], [l2, r2](1 <= l1 <= r1 <= n, 1 <= l2 <= r2 <= n),使得:

1)两个区间含有交集。即存在 x 满足 l1 <= x <= r1 且 l2 <= x <= r2。

2)a[l1...r1] + b[l2...r2] = s。

原题传送门。

@solution@

区间要有交集,可以等价认为是 l1 <= r2 且 l2 <= r1。

由题,区间长度和 (r1 - l1 + 1) + (r2 - l2 + 1) = m。

变形一下得到 (r2 - l1 + 1) + (r1 - l2 + 1) = m。

结合上面要有交集的不等式,可以得到 1 <= (r2 - l1 + 1) < m。

我们使用 z-algorithm 求出 a 中每一个 l1 与 s 的最长公共前缀,求出 b 中每一个 r2 与 s 的最长公共后缀。

接着,从后往前扫描 a 中的每一个 l1,维护 x[i] 表示如果 r1 - l1 + 1 = i 时对应了多少 r2。查询只需要区间求和即可。

因为 1 <= (r2 - l1 + 1) < m,得到 r2 的取值范围实际上是个滑动的区间。每一个 r2 的贡献是一个区间加。

因此直接用树状数组即可。

@accepted code@

#include <cstdio>
#include <algorithm>
using namespace std; typedef long long ll; const int MAXN = 500000;
const int MAXM = 2*MAXN; int z[MAXM + MAXN + 5];
char str[MAXM + MAXN + 5]; void getZ(int len) {
int pos = 0, mxl = -1;
for(int i=1;i<len;i++) {
z[i] = (mxl >= i ? min(mxl - i + 1, z[i - pos]) : 0);
while( str[z[i]] == str[i+z[i]] ) z[i]++;
if( mxl < i + z[i] - 1 ) pos = i, mxl = i + z[i] - 1;
}
}
void get(char *S, int lenS, char *T, int lenT, int *f) {
for(int i=0;i<lenS;i++) str[i] = S[i];
str[lenS] = 1;
for(int i=0;i<lenT;i++) str[lenS+1+i] = T[i];
getZ(lenS + lenT + 1);
for(int i=0;i<lenT;i++) f[i] = z[lenS+1+i];
} int n, m; ll T[2][MAXM + 5];
int lowbit(int x) {return (x & -x);}
void add(int x, ll d, int t) {
for(int i=x;i<=m;i+=lowbit(i))
T[t][i] += d;
}
void modify(int l, int r, ll d) {
add(l, d, 0), add(l, d*l, 1);
add(r + 1, -d, 0), add(r + 1, -d*(r + 1), 1);
}
ll sum(int x, int t) {
ll ret = 0;
for(int i=x;i;i-=lowbit(i))
ret += T[t][i];
return ret;
}
ll query(int l, int r) {
ll A = l*sum(l - 1, 0) - sum(l - 1, 1);
ll B = (r + 1)*sum(r, 0) - sum(r, 1);
return B - A;
} char a[MAXN + 5], b[MAXN + 5], s[MAXM + 5];
int af[MAXN + 5], bf[MAXN + 5];
int main() {
scanf("%d%d%s%s%s", &n, &m, a, b, s);
get(s, m, a, n, af);
reverse(b, b + n), reverse(s, s + m);
get(s, m, b, n, bf);
reverse(bf, bf + n);
ll ans = 0;
for(int i=n-1;i>=0;i--) {
modify(max(m - bf[i], 1), m - 1, 1);
if( i + m - 1 < n )
modify(max(m - bf[i + m - 1], 1), m - 1, -1);
ans += query(1, af[i]);
}
printf("%lld\n", ans);
}

@details@

小清新的题目。

差点忘了树状数组的区间加与区间求和怎么写了。。。

@codefoces - 1313E@ Concatenation with intersection的更多相关文章

  1. [LeetCode] Intersection of Two Arrays II 两个数组相交之二

    Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...

  2. [LeetCode] Intersection of Two Arrays 两个数组相交

    Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...

  3. [LeetCode] Intersection of Two Linked Lists 求两个链表的交点

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  4. [LeetCode] Substring with Concatenation of All Words 串联所有单词的子串

    You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...

  5. 【leetcode】Intersection of Two Linked Lists

    题目简述: Write a program to find the node at which the intersection of two singly linked lists begins. ...

  6. [LintCode] Intersection of Two Linked Lists 求两个链表的交点

    Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...

  7. LeetCode Intersection of Two Arrays

    原题链接在这里:https://leetcode.com/problems/intersection-of-two-arrays/ 题目: Given two arrays, write a func ...

  8. Leetcode Substring with Concatenation of All Words

    You are given a string, S, and a list of words, L, that are all of the same length. Find all startin ...

  9. Intersection of Two Linked Lists

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

随机推荐

  1. opencv3学习1:opencv3.4.10与vs2017环境配置

    原教程网址:https://jingyan.baidu.com/article/dca1fa6f13bd55f1a44052b9.html 具体教程网上很多,我也相信大家的搜素能力,作为一个初入C++ ...

  2. css不换行解决

    word-wrap: break-word; word-break: break-all; white-space: pre-wrap;

  3. 像宝石一样的Java原子类

    十五年前,多处理器系统是高度专业化的系统,通常耗资数十万美元(其中大多数具有两到四个处理器). 如今,多处理器系统既便宜又丰富,几乎主流的微处理器都内置了对多处理器的支持,很多能够支持数十或数百个处理 ...

  4. C#基础之方法的重载

    在C#语言中,方法的重载作用非常大,但是使用重载需要注意方法的签名,必须有一种要不一样,具体指的是: 1.方法的返回值类型 2.方法的形参类型 3.形参类型的顺序 4.形参的个数 4.泛型的类型< ...

  5. 百度地图结合ECharts实现复杂覆盖物(Overlay)

    先来看效果图 一 前置知识 官方Overlay-覆盖物的抽象基类 方法 返回值 描述 initialize(map: Map) HTMLElement 抽象方法,用于初始化覆盖物,当调用map.add ...

  6. Java并没有衰落.大家对它的认识才刚刚开始 Java8全新出发

    Java并没有衰落.大家对它的认识才刚刚开始 很高兴能在此给大家分享Java8的新特性.这篇文章将一步一步带你了解Java8的所有新特性.我将通过简单的实例代码向大家展示接口中默认方法,lambda ...

  7. 接口(interface)的使用

    类实现接口就具有接口的功能 实现可以多实现,实现多个接口 package cm.aff.abst; /* 接口(interface)是与类并行的一个概念 1. 接口可以看做是一个特殊的抽象类,是常量与 ...

  8. Rocket - tilelink - CrossingHelper

    https://mp.weixin.qq.com/s/y432EkLcBvVn2u_U3tPWeA   简单介绍CrossingHelper的实现.   ​​   1. 基本介绍   为节点生成一个跨 ...

  9. Java实现 LeetCode 778 水位上升的泳池中游泳(二分+DFS)

    778. 水位上升的泳池中游泳 在一个 N x N 的坐标方格 grid 中,每一个方格的值 grid[i][j] 表示在位置 (i,j) 的平台高度. 现在开始下雨了.当时间为 t 时,此时雨水导致 ...

  10. Java实现 蓝桥杯 算法训练 Beaver's Calculator

    试题 算法训练 Beaver's Calculator 问题描述 从万能词典来的聪明的海狸已经使我们惊讶了一次.他开发了一种新的计算器,他将此命名为"Beaver's Calculator ...