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. 使用 java.util.Properties 读取配置文件中的参数

    配置文件格式 如下的配置参数格式都支持: Key = ValueKey = Key:ValueKey :Value 用法 getProperty方法的返回值是String类型. //读取配置文件 Fi ...

  2. Spring Boot 为什么这么火?(二)

    Spring Boot 的火 网上连载了 Spring Boot 系列文章,没想到这一开始便与 Spring Boot 深度结缘. 技术社区 Spring Boot 的文章越来越多:Spring Bo ...

  3. 【C语言--数据结构】线性表链式存储结构

    直接贴代码 头文件 #ifndef __LINKLIST_H__ #define __LINKLIST_H__ typedef void LinkList; typedef struct _tag_L ...

  4. 关于连接池和DBUtils

    1.关于数据库连接池 连接池就是创建和管理一个连接缓冲池的技术,这些连接好被任意线程所使用,数据库连接池负责分配和管理.释放数据库连接,总而言之数据库连接池就是提高对数据库的操作性能. 数据库连接池: ...

  5. tz汇报

    不爽,不满意,存在太多Bug,汇报前的了解不充分,了解到了有那些领导参加,但是没有具体了解领导的时间安排,没有按照领导的时间调整汇报提纲及思路,汇报到1个半小时,领导需要参加会议,提前离开,没能够与领 ...

  6. 封装 多态 类的约束 super

    python面向对象的三大特性:继承,封装,多态. 1. 封装: 把很多数据封装到⼀个对象中. 把固定功能的代码封装到⼀个代码块, 函数, 对象, 打包成模块. 这都属于封装的思想. 具体的情况具体分 ...

  7. Print out Android kernel log

    adb shell "su -c 'cat /proc/kmsg'" | tee kernel.log adb shell cat /proc/last_kmsg > las ...

  8. 剑指offer-树的子结构-python

    题目描述 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构)     思路 空树不是任意一个树的子结构,如果 root1 与root2 中有一个为空树的话,返回 ...

  9. spring boot 枚举使用的坑2

    上一篇说到在枚举当在controller的方法做参数时的坑,解决方法是配置了一个converter,后来想想,如果不闲每次都加一个注解麻烦的话,可以在参数前面加一个注解,添加一个解析器应该也可以解决这 ...

  10. Django之cookie 和session

    ---恢复内容开始--- 一.cookie 前戏.cookie 的由来 由于http协议是无状态的 无法记录用户状态 cookie就是保存在客户端浏览器上的键值对 工作原理:当你登陆成功之后 浏览器会 ...