题目传送门

题意简述:给出 \(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. cunda 常用命令,删除,创建,换源

    https://github.com/tensorflow/tensorflow/ conda create --name [虚拟环境名] python=3.7 创建一个环境 conda activa ...

  2. 验证域用户(C#)

    代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Intero ...

  3. SpringCloud微服务实战——搭建企业级开发框架(七):自定义通用响应消息及统一异常处理

      平时开发过程中,无可避免我们需要处理各类异常,所以这里我们在公共模块中自定义统一异常,Spring Boot 提供 @RestControllerAdvice 注解统一异常处理,我们在GitEgg ...

  4. GPIO原理与配置(跑马灯,蜂鸣器,按键)

    一.STM32 GPIO固件库函数配置方法 1. 根据需要在项目中删掉一些不用的固件库文件,保留有用的固件库文件 2. 在stm32f10x_conf.h中注释掉这些不用的头文件 3. STM32的I ...

  5. 你知道怎么从jar包里获取一个文件的内容吗

    目录 背景 报错的代码 原先的写法 编写测试类 找原因 最终代码 背景 项目里需要获取一个excle文件,然后对其里的内容进行修改,这个文件在jar包里,怎么尝试都读取不成功,但是觉得肯定可以做到,因 ...

  6. 『学了就忘』Linux基础 — 10、VMware虚拟机中克隆的使用

    目录 1.什么是克隆 2.克隆的两种类型 (1)完整克隆 (2)链接克隆 3.克隆操作 步骤一:克隆虚拟机 步骤二:进行克隆导向 3.快照与克隆的区别 4.镜像的管理 快照和克隆是VMware中两个非 ...

  7. simulate_click

    #!/bin/bashlet actual_x=104+144*$[$2-1]let actual_y=945+144*$[$1-1]adb shell input tap ${actual_x} $ ...

  8. poj 3417 Network (LCA,路径上有值)

    题意: N个点,构成一棵树.给出这棵树的结构. M条边,(a1,b1)...(am,bm),代表给树的这些点对连上边.这样就形成了有很多环的一个新"树". 现在要求你在原树中断一条 ...

  9. 如何反编译微信小程序👻

    如何反编译微信小程序 准备工具: 夜神模拟器(或者你可以自己准备一个安卓模拟器,有root权限.) RE文件管理器(下载地址:https://soft.ucbug.com/uploads/shouji ...

  10. 开源项目|Go 开发的一款分布式唯一 ID 生成系统

    原文连接: 开源项目|Go 开发的一款分布式唯一 ID 生成系统 今天跟大家介绍一个开源项目:id-maker,主要功能是用来在分布式环境下生成唯一 ID.上周停更了一周,也是用来开发和测试这个项目的 ...