@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. 利用logrotate将mysql log截断

    https://blog.pythian.com/mysql-log-rotation/ 1.授权用户 CREATE USER 'log_rotate'@'localhost' IDENTIFIED ...

  2. haproxy mycat mysql 读写分离MHA高可用

    主机IP信息 hostname IP 172.16.3.140 haproxy01 172.16.3.141 haproxy02 172.16.3.142 mycat01 172.16.3.143 m ...

  3. 可持续字典树 Perfect Security

    题目链接 题目大意:给你两个序列,第二个序列可以任意进行排列变换,然后由这两个序列一一异或得到答案序列,要求答案序列的字典序最小. 可持续字典树与第K大可持续线段树的区别主要在于每个节点上 ,它多了一 ...

  4. BZOJ1078 斜堆

    http://hzwer.com/5790.html  代码 http://www.cppblog.com/MatoNo1/archive/2013/03/03/192131.html  //原理讲解 ...

  5. Angular SPA基于Ocelot API网关与IdentityServer4的身份认证与授权(一)

    好吧,这个题目我也想了很久,不知道如何用最简单的几个字来概括这篇文章,原本打算取名<Angular单页面应用基于Ocelot API网关与IdentityServer4+ASP.NET Iden ...

  6. Centos7 安装完以后安全配置

    1.更新系统和补丁 我们的互联网是很不安全的,每天都有新的漏洞出现和修复,所以一定要更新.更新.更新, yum -y update 上面的命令是检查更新并安装,包括内核和软件,建议刚安装完就更新一次, ...

  7. 【Copy攻城狮日志】Node快速重命名文件,告别Potplay字幕困扰问题

    ↑开局一张图,故事全靠编↑ 前言 Copy攻城狮日志的惯例,开局一张图,开始为您讲述一个鲜为人知的故事.故事的开头要从本大狮从盗版网站下载udemy课程的犯罪伊始说起,去年的某月某天,我真正接触到了“ ...

  8. Django创建应用以及路由的配置

    Django简介:是一个开放源代码的web 应用框架,由python 写成.初次发布于2005年7月,并于2008年9月发布了第一个正式版本1.0. 文件说明:manage.py:一个命令行工具,可以 ...

  9. Network Motif 文献调研

    Network Motif 文献调研 概述:Network motifs,可以认为是网络中频繁出现的子图模式,是复杂网络的"构建块".有两篇发表在science上的论文给出moti ...

  10. Java IO(二)File

    Java IO(二)File 一.概述 在 Java 中,File 类是 java.io 包中唯一代表磁盘文件本身的对象,也就是说,如果希望在程序中操作文件和目录,则都可以通过 File 类来完成.F ...