@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. mysql小白系列_08 zabbix3.2.6概念及部署

    一 zabbix功能简介 1.zabbix三大监控组件 zabbix server web gui database zabbix_server zabbix proxy agent client 2 ...

  2. 推荐一款复式记账软件——GnuCash

    本文需要搞清楚两个事情,第一,什么是复式记账:第二,GnuCash操作 复式记账,来自百度百科的解释:复式记账法是以资产与权益平衡关系作为记账基础,对于每一笔经济业务,都要以相等的金额在两个或两个以上 ...

  3. NPM的由来——为什么要使用NPM

    原文:https://blog.csdn.net/qq_37696120/article/details/80507178 https://blog.csdn.net/hong10086/articl ...

  4. JAVA定义变量和基础的数据类型和关键字

      标识符语法 1)java中定义标识符格式以字母,数字,下划线,$符合组成,不能以数字开头,且不能为  java中的关键字.         2)标识符意义要明确,不要乱起         3)标识 ...

  5. 二维DCT变换 | Python实现

    引言 最近专业课在学信息隐藏与数字水印,上到了变换域隐藏技术,提到了其中的DCT变换,遂布置了一个巨烦人的作业,让手动给两个\(8\times8\)的矩阵做二维DCT变换,在苦逼的算了一小时后,我决定 ...

  6. ActiveMQ 反序列化漏洞(CVE-2015-5254)复现

    1.运行漏洞环境 sudo docker-compose up -d 环境运行后,将监听61616和8161两个端口.其中61616是工作端口,消息在这个端口进行传递:8161是Web管理页面端口.访 ...

  7. 乌云jsonp案例

    新浪微博之点击我的链接就登录你的微博(JSONP劫持) 生活处处有惊喜啊!逛逛wooyun都能捡到bug. 测试的时候没关burp,逛乌云的时候抓到一条url: http://login.sina.c ...

  8. vnc下载 vnc下载-安装-登录教程(超详细!)

    许多人在服务器下载了vnc服务端却不知道使用.下面我来教你使用方法 本次教程使用的工具:iis7服务器管理 一.首先,用iis7服务器管理工具登录需要下载vnc服务端的服务器,完成下载. 1.iis7 ...

  9. 前端和Nodejs的关系 简单理解

    前端使用JS脚本语言进行开发. JS脚本语言需要依赖一个平台运行,从而生成可视化的东西. Node.js提供这个平台,同时提供JS运行需要的一些插件.库.包.轮子.组件.功能等等. JavaScrip ...

  10. Rocket - tilelink - Metadata

    https://mp.weixin.qq.com/s/Dyb1XipJtdhGa9mktXXjCg   简单介绍Metadata的实现.   ​​   1. 基本介绍   Metadata是一个Bun ...