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...的更多相关文章

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

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

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

  3. Multiply Strings

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

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

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

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

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

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

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

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

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

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

  9. 使用strings查看二进制文件中的字符串

    使用strings查看二进制文件中的字符串 今天介绍的这个小工具叫做strings,它实现功能很简单,就是找出文件内容中的可打印字符串.所谓可打印字符串的涵义是,它的组成部分都是可打印字符,并且以nu ...

  10. LeetCode 205 Isomorphic Strings

    Problem: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if ...

随机推荐

  1. 20191112 Spring Boot官方文档学习(4.3)

    4.3.Profiles Spring Profiles提供了一种隔离部分应用程序配置并使之仅在某些环境中可用的方法.任何@Component,@Configuration或@Configuratio ...

  2. VS2013启动 外接程序VMDebugger未能加载或导致了异常

    故障现象:打开Visual Studio 2010后弹出错误框,外接程序VMDebugger未能加载或导致了异常,是否希望移除该外接程序,错误号:80004005.系统版本:WIN10 64位专业版, ...

  3. 部署第一个servlet应用到tomcat上

    部署第一个servlet应用到tomcat上 搭建环境 eclipse的EE版本 eclipse官网 创建一个java的应用程序 1.File->new java project 导入servl ...

  4. P1168 中位数 堆

    题目描述 给出一个长度为NN的非负整数序列A_iAi​,对于所有1 ≤ k ≤ (N + 1) / 21≤k≤(N+1)/2,输出A_1, A_3, …, A_{2k - 1}A1​,A3​,…,A2 ...

  5. c语言中不允许在函数外部给全局变量赋值

    今天,在写条件编译的时候,出现了在函数外部给全局变量赋值的情况,gcc报错,那么c语言为什么不允许在函数外部给变量赋值呢?为什么声明变量的时候可以对变量进行赋值? 出错代码: /* 2 * ===== ...

  6. Ajax&Json笔记

    ## 今日内容     1. AJAX:     2. JSON # AJAX:     1. 概念: ASynchronous JavaScript And XML    异步的JavaScript ...

  7. webpack output的path publicPath

    path是用来定义入口文件保存的地址,而publicPath是定义非入口文件需要抽离保存的第三方文件的保存地址 vue-cli 中HtmlWebpackPlugin生成html,都会存在path路径上 ...

  8. bak文件恢复成 SQL2005 数据库 的全程讲解

    经常会碰到客户给我们发的是一个bak的数据库备份文件,而不是一个数据库.这就需要我们把这数据文件还原成数据库的形式. 如将demo.bak数据库恢复到mssql2005下 打开SQL2005,打开后就 ...

  9. Qt下载(多种下载通道+所有版本)

    http://c.biancheng.net/view/3851.html Qt 体积很大,有 1GB~3GB,官方下载通道非常慢,相信很多读者会崩溃,所以建议大家使用国内的镜像网站(较快),或者使用 ...

  10. Python安装模块包

    可以利用pycharm安装模块包 使用这种方法安装时,可能会报下面类型的异常 AttributeError: module 'pip' has no attribute 'main' 出现这这样的异常 ...