CF1202E You Are Given Some Strings...
题意简述:给出 \(t\) 与 \(s_{1,2,\cdots,n}\)。求对于所有 \(i,j\in[1,n]\),\(s_i+s_j\) 在 \(t\) 中出现次数之和。
如果只有 \(s_i\) 那么显然是 ACAM 的板子题,对每个位置 \(p\) 记录它的前缀的所有后缀能与多少 \(s_i\) 相等,即为 \(a_p\)(即有多少 \(i\) 满足 \(t[p-|s_i|+1:p]=s_i\)。实际上就是该位置前缀 \(t[1:p]\) 在 ACAM 上跑到的位置在 fail 树上与根的路径上有多少 \(s_i\) 的终止节点)。如果再加上 \(s_j\),就要求一个位置的后缀有多少与 \(s_j\) 相等的前缀,即为 \(b_p\)。那么可以建 \(s\) 所有反串的 ACAM,再用 \(t\) 的反串上去跑即可。根据乘法原理,答案为 \(\sum a_ib_{i+1}\)。
时间复杂度为字符总长度乘以字符集大小。
/*
Powered by C++11.
Author : Alex_Wei.
*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) x.begin(),x.end()
#define rev(x) reverse(all(x))
const int N=2e5+5;
const int S=26;
struct ACAM{
int cnt,f[N],son[N][S],ed[N];
void ins(string s){
int p=0;
for(char it:s){
if(!son[p][it-'a'])son[p][it-'a']=++cnt;
p=son[p][it-'a'];
} ed[p]++;
} void build(){
queue <int> q;
for(int i=0;i<26;i++)if(son[0][i])q.push(son[0][i]);
while(!q.empty()){
int t=q.front(); q.pop();
for(int i=0;i<26;i++)
if(son[t][i])f[son[t][i]]=son[f[t]][i],q.push(son[t][i]);
else son[t][i]=son[f[t]][i];
ed[t]+=ed[f[t]];
}
}
}a,b;
ll n,ans,s[N];
string t;
int main(){
cin>>t>>n;
for(int i=1;i<=n;i++){
string s; cin>>s;
a.ins(s),rev(s),b.ins(s);
} a.build(),b.build();
for(int i=1,p=0;i<=t.size();i++)
p=a.son[p][t[i-1]-'a'],s[i]=a.ed[p];
for(int i=t.size(),p=0;i;i--)
p=b.son[p][t[i-1]-'a'],ans+=s[i-1]*b.ed[p];
cout<<ans<<endl;
return 0;
}
CF1202E You Are Given Some Strings...的更多相关文章
- ACAM 题乱做
之前做了不少 ACAM,不过没怎么整理起来,还是有点可惜的. 打 * 的是推荐一做的题目. I. *CF1437G Death DBMS 见 我的题解. II. *CF1202E You Are Gi ...
- 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 ...
随机推荐
- 什么,你还使用 webpack?别人都在用 vite 搭建项目了
一.vite 到底是干嘛的? vite 实际上就是一个面向现代浏览器,基于 ES module 实现了一个更轻快的项目构建打包工具. vite 是法语中轻快的意思. vite 的特点: 1.轻快的冷服 ...
- 第5次 Beta Scrum Meeting
本次会议为Beta阶段第6次Scrum Meeting会议 会议概要 会议时间:2021年6月6日 会议地点:「腾讯会议」线上进行 会议时长:10min 会议内容简介:对完成工作进行阶段性汇报:对下一 ...
- Noip模拟72 2021.10.9
T1 出了个大阴间题 真就以为他出了个大阴间题就没写,打个暴力就跑了 数据范围显然摆明是状压 设$f[sta][0/1]$表示在已经选择的集合$sta$中,$A$的最大值是$A$还是$A+1$ 然后按 ...
- [luogu2973]driving out the piggies 驱逐猪猡【高斯消元+概率DP】
看到题面的那一刻,我是绝望的ORZ 图论加概率期望加好像不沾边的高斯消元???我人直接傻掉 还没学过概率期望的我果断向题解屈服了(然后还是傻掉了两节课来找线性方程.. Description 奶牛们建 ...
- 函数指针和qsort函数
1.函数指针的形式: 函数指针:int (*funcP) (int *a, int *b) 表示定义了一个funcP函数指针,指向了返回值为int类型,参数为int* 和int* 的函数 使用方式: ...
- 攻防世界 杂项 6.pure_color
图片隐写 工具 使用StegSolve一把梭 另一种解法 右击图片编辑,画图工具打开,属性设置黑白.
- 绑定socket描述符到一个网络设备
网络编程中有时明明用eth0的地址来bind一个udp套接口, 可是发出去的包却是从eht1走的, 在网上找到这么一段话解释该问题: 在多 IP/网卡主机上,UDP ...
- centos 下安装docker
官方文档比较累赘,简化就三步 1.安装依赖 yum -y install gcc gcc-c++ yum-utils device-mapper-persistent-data lvm2 2.添加re ...
- 【java+selenium3】多窗口window切换及句柄handle获取(四)
一 .页面准备 1.html <html> <head> <title>主页面 1</title> </head> <body> ...
- adb 安装与使用(一)
一.ADB简介 1. 什么是adb? adb(Android Debug Bridage)是Android sdk的一个工具: adb 是用来连接安卓手机和PC端的桥梁,要有adb作为二者之间的维系, ...