[CF452E]Three strings
题目大意:给你三个字符串$A,B,C$,令$L=min(|A|,|B|,|C|)$,对每个$i\in[1,L]$,求出符合$A_{[a,a+i)}=B_{[b,b+i)}=C_{[c,c+i)}$的三元组$(a,b,c)$的个数
题解:先建一棵广义$SAM$,求出每个点可以到达的$A,B,C$的字串的个数,然后这个点贡献的值就是三个串分别的个数乘起来,发现一个点会对$[R_{fail_p+1},R_{p}]$的区间产生贡献,可以差分一下维护。
卡点:无
C++ Code:
#include <algorithm>
#include <cstdio>
#include <cstring>
#define maxn 300010
const int mod = 1e9 + 7;
inline void reduce(int &x) {x += x >> 31 & mod;} int n = 0x3f3f3f3f;
int ans[maxn];
namespace SAM {
#define N (maxn * 3 << 1)
int R[N], fail[N], nxt[N][26];
int lst = 1, idx = 1, sz[N][3]; void append(int ch, int tg) {
int p = lst, np = lst = ++idx; R[np] = R[p] + 1, sz[np][tg] = 1;
for (; p && !nxt[p][ch]; p = fail[p]) nxt[p][ch] = np;
if (!p) fail[np] = 1;
else {
int q = nxt[p][ch];
if (R[p] + 1 == R[q]) fail[np] = q;
else {
int nq = ++idx;
fail[nq] = fail[q], R[nq] = R[p] + 1, fail[q] = fail[np] = nq;
std::copy(nxt[q], nxt[q] + 26, nxt[nq]);
for (; p && nxt[p][ch] == q; p = fail[p]) nxt[p][ch] = nq;
}
}
} int head[N], cnt;
struct Edge {
int to, nxt;
} e[N];
inline void addedge(int a, int b) {
e[++cnt] = (Edge) {b, head[a]}; head[a] = cnt;
} void dfs(int u) {
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
dfs(v);
sz[u][0] += sz[v][0], sz[u][1] += sz[v][1], sz[u][2] += sz[v][2];
}
}
void work() {
for (int i = 2; i <= idx; i++) addedge(fail[i], i);
dfs(1);
for (int i = 2; i <= idx; i++) {
int tmp = static_cast<long long> (sz[i][0]) * sz[i][1] % mod * sz[i][2] % mod;
reduce(ans[R[fail[i]] + 1] += tmp - mod), reduce(ans[R[i] + 1] -= tmp);
}
for (int i = 2; i <= n; i++) reduce(ans[i] += ans[i - 1] - mod);
}
#undef N
} char s[maxn];
int main() {
for (int i = 0; i < 3; i++) {
scanf("%s", s); SAM::lst = 1;
n = std::min(n, static_cast<int> (strlen(s)));
for (char *ch = s; *ch; ++ch) SAM::append(*ch - 'a', i);
}
SAM::work();
for (int i = 1; i <= n; i++) {
printf("%d", ans[i]);
putchar(i == n ? '\n' : ' ');
}
return 0;
}
[CF452E]Three strings的更多相关文章
- CF452E Three strings 广义后缀自动机
建一个广义后缀自动机统计一下就行,好长时间不敲后缀自动机调了半天~ #include <bits/stdc++.h> using namespace std; namespace IO { ...
- Hacker Rank: Two Strings - thinking in C# 15+ ways
March 18, 2016 Problem statement: https://www.hackerrank.com/challenges/two-strings/submissions/code ...
- StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing the strings?
StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing t ...
- Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- [LeetCode] Add Strings 字符串相加
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...
- [LeetCode] Encode and Decode Strings 加码解码字符串
Design an algorithm to encode a list of strings to a string. The encoded string is then sent over th ...
- [LeetCode] Group Shifted Strings 群组偏移字符串
Given a string, we can "shift" each of its letter to its successive letter, for example: & ...
- [LeetCode] Isomorphic Strings 同构字符串
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- [LeetCode] Multiply Strings 字符串相乘
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
随机推荐
- SpringBoot-07:SpringBoot整合PageHelper做多条件分页查询
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本篇博客讲述如何在SpringBoot中整合PageHelper,如何实现带多个条件,以及PageInfo中的 ...
- vue中开发webSocket
先安装 sockjs-client 和 stompjs npm install sockjs-client npm install stompjs <template> <div&g ...
- 基于Impala平台打造交互查询系统
本文来自网易云社区 原创: 蒋鸿翔 DataFunTalk 本文根据网易大数据蒋鸿翔老师DataFun Talk--"大数据从底层处理到数据驱动业务"中分享的<基于Impal ...
- TraceHelper
public class TraceHelper { private static TraceHelper _traceHelper; private TraceHelper() { } public ...
- Objective-C 第一个小程序
示例一 (类似C) //1.代码编写 //跟C语言一样,OC程序的入口依然是main函数,只不过写到一个.m文件中.比如这里写到一个main.m文件中(文件名可以是中文) #include <s ...
- 2018牛客多校第二场a题
一个人可以走一步或者跳x步,但不能连着跳,问到这个区间里有几种走法 考虑两种状态 对于这一点,我可以走过来,前面是怎么样的我不用管,也可以跳过来但是,跳过来必须保证前一步是走的 dp[i][0]表示 ...
- UVa 1586 - Molar Mass - ACM/ICPC Seoul 2007 - C语言
关键在于判断数字是两位数还是单位数,其他部分没有难度. #include"stdio.h" #include"string.h" #include"c ...
- 十 Writing YARN Applications
本节介绍: 使用yarn 高级提交写yarn应用程序.其实已经yarn底层API.MR计算框架对底层的API实现了封装. 高级提交指直接使用yarn的三种接口来提交应用程序: 1)YarnCl ...
- URAL 1664 Pipeline Transportation(平面图最大流)
Description An oligarch Vovan, as many other oligarchs, transports oil from West Cuckooland to East ...
- Lecture Sleep(尺取+前缀和)
Description 你的朋友Mishka和你参加一个微积分讲座.讲座持续n分钟.讲师在第i分钟讲述ai个定理. 米什卡真的对微积分很感兴趣,尽管在演讲的所有时间都很难保持清醒.给你一个米什卡行 ...