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 ...
随机推荐
- 20191127 Spring Boot官方文档学习(6-8)
6.部署Spring Boot应用程序 在部署应用程序时,Spring Boot的灵活打包选项提供了很多选择.您可以将Spring Boot应用程序部署到各种云平台,容器映像(例如Docker)或虚拟 ...
- Springboot2.x集成Redis集群模式
Springboot2.x集成Redis集群模式 说明 Redis集群模式是Redis高可用方案的一种实现方式,通过集群模式可以实现Redis数据多处存储,以及自动的故障转移.如果想了解更多集群模式的 ...
- [P5348]密码解锁
Description 给一个长度为 \(n\) 的数组 \(a[1\dots n]\) ,满足 \(\sum_{m|x}a[x] = \mu(m)\),求 \(a[m]\). \(n\le 10^{ ...
- npm errno -4048错误
这种错误是缓存原因导致的,首先清除缓存 npm cache clean --force 然后校验缓存依赖的完整和有效性 npm cache verify 最后重新安装即可
- Windows 10 IoT Core Dashboard 无法安装的问题
有人在answers.microsoft.com问这个问题,官方给了个这样的回答,然后还锁定了问题不让别人回复 您好, 了解到您在使用时遇到问题. 请您详细描述下您的操作,请问您是在打开安装程序还是在 ...
- 小程序-调用公共js对象方法/ app.js
在小程序中,如果在子页面想调用共公js的方法,需先在子页面js中先实例化app:具体过程如下 子页面js: 1 2 3 4 5 6 7 8 //调用公共js对象以便调用其方法 var app = ge ...
- ES6初步学习
from:http://www.jianshu.com/p/287e0bb867ae 刚开始用vue或者react,很多时候我们都会把ES6这个大兄弟加入我们的技术栈中.但是ES6那么多那么多特性,我 ...
- 利用webSocket实现浏览器中多个标签页之间的通信
webSoket用来实现双向通信,客户端和服务端实时通信. webSoket优点和缺点? 优点:对于前端来说,使用简单,功能灵活,如果部署了webSocket服务器,可以实现实时通信. 缺点:需要服务 ...
- MySQL索引优化(索引单表优化案例)
1.单表查询优化 建表SQL CREATE TABLE IF NOT EXISTS `article` ( `id` INT(10) UNSIGNED NOT NULL PRIMARY KEY AUT ...
- 免费申请 QQ 免费靓号
打开网址:https://ssl.zc.qq.com/v3/index-chs.html?type=3 说明:靓号一般分为 9位靓号 or 带有寓意的号码 激活规则:获取的"靓号" ...