E. You Are Given Some Strings...
E. You Are Given Some Strings...
AC自动机
求一个串$t$中包含子串$s_{i}+s_{j}$的个数。
可以正反跑两遍AC自动机
正着跑,表示$s_{i}$结束,反正跑对应$s_{i}$开头
#include<bits/stdc++.h>
using namespace std;
#define maxn 1000005
#define maxm 28
struct AC{
int trieN;
int ch[maxn][maxm];
int val[maxn],tt[maxn];
int fail[maxn];
int sum[maxn];
vector<int> v;
void init()
{
trieN=-;
newnod();
}
int newnod()
{
memset(ch[++trieN],,sizeof ch[]);
val[trieN]=fail[trieN]=;
return trieN;
}
void insert(const string & str)
{
int cur=;
for(int i=;i<str.size();i++){
int d=str[i]-'a';
if(!ch[cur][d]){
ch[cur][d]=newnod();
}
cur=ch[cur][d];
}
val[cur]++;
}
void build()
{
queue<int> q;
for(int i=;i<maxm;i++){
if(ch[][i]){
q.push(ch[][i]);
}
}
while(!q.empty()){
int cur=q.front();
v.push_back(cur);
q.pop();
for(int i=;i<maxm;i++){
if(ch[cur][i]){
fail[ch[cur][i]]=ch[fail[cur]][i];
q.push(ch[cur][i]);
}else{
ch[cur][i]=ch[fail[cur]][i];
}
}
}
for(int i=;i<v.size();i++){
int u=v[i];
tt[u]=tt[fail[u]]+val[u];
}///优化??
} void query(const string & str)
{ int res=,cur=;
for(int i=;str[i];i++){
int d=str[i]-'a';
cur=ch[cur][d]; sum[i]=tt[cur];
res=;
}
//return res;
}
}ac1,ac2; int main()
{
string s,t;
cin>>t;
int n;
scanf("%d",&n);
for(int i=;i<n;i++){
cin>>s; ac1.insert(s);
reverse(s.begin(),s.end());
ac2.insert(s);
}
ac1.build();
ac2.build();
ac1.query(t);
long long ans=;
reverse(t.begin(),t.end());
ac2.query(t);
int len=t.length();
/*for(int i=0;i<len;i++){
cout<<ac1.sum[i]<<" "<<ac2.sum[i]<<endl;
}*/
// cout<<len<<'\n';
for(int i=;i<=len;i++){
ans+=(long long)ac1.sum[i-]*ac2.sum[len-i-];
}
cout<<ans<<'\n';
}
E. You Are Given Some Strings...的更多相关文章
- 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 ...
- 使用strings查看二进制文件中的字符串
使用strings查看二进制文件中的字符串 今天介绍的这个小工具叫做strings,它实现功能很简单,就是找出文件内容中的可打印字符串.所谓可打印字符串的涵义是,它的组成部分都是可打印字符,并且以nu ...
- LeetCode 205 Isomorphic Strings
Problem: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if ...
随机推荐
- 复制/etc目录下所有以p开头,以非数字结尾的文件或目录到/tmp/mytest1目录中
cp -r /etc/p*[a-Z] /tmp/mytest1
- python并发编程之进程池、线程池、协程
需要注意一下不能无限的开进程,不能无限的开线程最常用的就是开进程池,开线程池.其中回调函数非常重要回调函数其实可以作为一种编程思想,谁好了谁就去掉 只要你用并发,就会有锁的问题,但是你不能一直去自己加 ...
- 【监控笔记】【1.1】监控事件系列——SQL Server Profiler
声明:本系列是书目<sql server监控与诊断读书笔记> 联机丛书监控:https://docs.microsoft.com/en-us/sql/relational-database ...
- Sql server 执行计划详解
序言 本篇主要目的有二: 1.看懂t-sql的执行计划,明白执行计划中的一些常识. 2.能够分析执行计划,找到优化sql性能的思路或方案. 如果你对sql查询优化的理解或常识不是很深入,那么推荐几骗博 ...
- nrm安装与配置
nrm安装与配置:https://blog.csdn.net/anway12138/article/details/79455224
- 教你在 IntelliJ IDEA 中使用 VIM!
Java技术栈 www.javastack.cn 优秀的Java技术公众号 IdeaVim(下载)插件可以让你在IntelliJ IDEA中键盘敲的飞起. 安装 打开IDEA的设置,在Plugins里 ...
- uboot第二阶段分析1
一. uboot第二阶段初识 1.1. uboot第二阶段应该做什么 a. 概括来讲uboot第一阶段主要就是初始化了SoC内部的一些部件(譬如看门狗.时钟),然后初始化DDR并且完成重定位. b. ...
- 面向对象super 练习
看代码写结果[如果有错误,则标注错误即可,并且假设程序报错可以继续执行] class Foo(object): a1 = 1 def __init__(self,num): self.num = nu ...
- python 如何解决高并发下的库存问题??
python 提供了2种方法解决该问题的问题:1,悲观锁:2,乐观锁 悲观锁:在查询商品储存的时候加锁 select_for_update() 在发生事务的commit或者是事务的rollback时 ...
- 如何将一个.NET Core类库发布到NuGet
包治百病 | 如何将一个.NET Core类库发布到NuGet 写文章 包治百病 | 如何将一个.NET Core类库发布到NuGet Edi Wang发表于汪宇杰博客订阅 77 NuGet是.NET ...