题目传送门

题意简述:给出 \(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...的更多相关文章

  1. ACAM 题乱做

    之前做了不少 ACAM,不过没怎么整理起来,还是有点可惜的. 打 * 的是推荐一做的题目. I. *CF1437G Death DBMS 见 我的题解. II. *CF1202E You Are Gi ...

  2. Hacker Rank: Two Strings - thinking in C# 15+ ways

    March 18, 2016 Problem statement: https://www.hackerrank.com/challenges/two-strings/submissions/code ...

  3. 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 ...

  4. Multiply Strings

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  5. [LeetCode] Add Strings 字符串相加

    Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...

  6. [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 ...

  7. [LeetCode] Group Shifted Strings 群组偏移字符串

    Given a string, we can "shift" each of its letter to its successive letter, for example: & ...

  8. [LeetCode] Isomorphic Strings 同构字符串

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  9. [LeetCode] Multiply Strings 字符串相乘

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

随机推荐

  1. 用css写三角形

    html部分 <div class="triangle></div> css部分 .triangle{ width:0; height:0; overflow:hid ...

  2. 第五课第四周笔记4:Transformer Network变压器网络

    Transformer Network变压器网络 你已经了解了 self attention,你已经了解了 multi headed attention.在这个视频中,让我们把它们放在一起来构建一个变 ...

  3. Flutter的环境配置以及一些常见问题

    flutter & AndroidStudio flutter的下载与配置 flutter是Google推出的基于Dart语言开发的跨平台开源UI框架,能够支持安卓与iOS. flutter框 ...

  4. 【二食堂】Alpha - Scrum Meeting 11

    Scrum Meeting 11 例会时间:4.21 18:00~18:20 进度情况 组员 进度 今日任务 李健 1. 登录注册页面前后端对接issue 1. 登录注册页面前后端对接issue2. ...

  5. 【JavaScript基础】Js的定时器(你想看的原理也在哟)

    [JavaScript基础]Js的定时器(你想看的原理也在哟) 博客说明 文章所涉及的资料来自互联网整理和个人总结,意在于个人学习和经验汇总,如有什么地方侵权,请联系本人删除,谢谢! 说明 本章是经历 ...

  6. .Net(c#)汉字和Unicode编码互相转换实例

    {"name": "\u676d\u5dde", "href": "www.baidu.com"} 经常遇到这样内容的j ...

  7. Python import urllib2 ImportError: No module named 'urllib2'

    python3 import urllib2 import urllib2 ImportError: No module named 'urllib2' python3.3里面,用urllib.req ...

  8. python生成有声小说模拟真人发音

    生成有声小说原理 文字是1500字内的生成微软文档说说 用代码实现小说爬取正本 实现每章小说1450字 实现自动剪切后添加封面 实现自动上传 用python代码实现爬取小说,本案列以一本小说为实列代码 ...

  9. linux系列之: 你知道查看文件空间的两种方法吗?

    目录 简介 du命令 df命令 总结 简介 linux系统中查看文件空间大小应该是一个非常常见的命令了,今天给大家介绍linux系统中查看文件空间的两种方法和在使用中可能会遇到的奇怪问题. 为什么会有 ...

  10. 【编译原理】LL1文法语法分析器

    上篇文章[编译原理]语法分析--自上向下分析 分析了LL1语法,文章最后说给出栗子,现在补上去. 说明: 这个语法分析器是利用LL1分析方法实现的. 预测分析表和终结符以及非终结符都是针对一个特定文法 ...