题目传送门

题意简述:给出 \(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. logging模块二

    背景,在学习logging时总是遇到无法理解的问题,总结,尝试一下更清晰明了了,让我们开始吧! logging模块常用format格式说明 %(levelno)s: 打印日志级别的数值 %(level ...

  2. 看动画学算法之:队列queue

    目录 简介 队列的实现 队列的数组实现 队列的动态数组实现 队列的链表实现 队列的时间复杂度 简介 队列Queue是一个非常常见的数据结构,所谓队列就是先进先出的序列结构. 想象一下我们日常的排队买票 ...

  3. DDD领域驱动设计-概述-Ⅰ

     如果我看得更远,那是因为我站在巨人的肩膀上.(If I have seen further it is by standing on ye shoulder of Giants.)         ...

  4. 2021北航敏捷软工Beta阶段评分与总结

    概述 Beta 阶段评分,按照之前的规则,主要组成部分为: 博客部分,基于 Beta 阶段博客的评分(每篇正规博客 10 分,每篇 Scrum5 分,评定方式类比往年) 评审部分,基于 Beta 阶段 ...

  5. 第1次 Beta Scrum Meeting

    本次会议为Beta阶段第1次Scrum Meeting会议 会议概要 会议时间:2021年5月29日 会议地点:「腾讯会议」线上进行 会议时长:0.5小时 会议内容简介:本次会议为Beta阶段第1次会 ...

  6. virtual box搭建虚拟机nat和host only网络配置实用

    virtual box搭建虚拟机nat和host only网络配置实用 一.背景 二.需求 二.设置虚拟机的网络 1.创建一个全局的nat网络 2.添加主机网络管理器 3.设置虚拟机网络 1.网卡1设 ...

  7. 热身训练2 GCD

    题目描述 简要题意:  n个数字,a1,a2,...,an m次询问(l,r),每次询问需回答 1.gcd(al,al+1,al+2,...,ar);2.gcd(ax,ax+1,ax+2,...,ay ...

  8. NOIP模拟22

    T1d 一道垃圾贪心,写个堆优化或者桶就行 1 #include<bits/stdc++.h> 2 #define int long long 3 using namespace std; ...

  9. Machine learning (7-Regularization)

    1.The Problem of Over-fitting 2.Cost Function 3.Regularized Linear Regression 4.Regularized Logistic ...

  10. MongoDB 集群 config server 查询超时导致 mongos 集群写入失败

    环境 OS:CentOS 7.x DB:MongoDB 3.6.12 集群模式:mongod-shard1 *3 + mongod-shard2 *3 + mongod-conf-shard *3 + ...