#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <string>
#include <queue>
using namespace std;
const int N=;
struct Tri
{
int Next[N][], fail[N], val[N], root, L;
void init() {L = ;root = newnode();}
int newnode()
{
for(int i = ;i < ;i++) Next[L][i] = -;
val[L++] = ;
return L - ;
} void insert(char s[])
{
int len = strlen(s), cur = root;
for(int i = ;i < len;i++)
{
if(Next[cur][s[i]-'a'] == -)
Next[cur][s[i]-'a'] = newnode();
cur = Next[cur][s[i]-'a'];
}
val[cur]++;
}
void build()
{
queue<int>Q;
fail[root] = root;
for(int i = ;i < ;i++)
if(Next[root][i] == -) Next[root][i] = root;
else
{
fail[Next[root][i]] = root;
Q.push(Next[root][i]);
}
while(!Q.empty())
{
int cur = Q.front(); Q.pop();
for(int i = ;i < ;i++)
if(Next[cur][i] == -)
Next[cur][i] = Next[fail[cur]][i];
else
{
fail[Next[cur][i]]=Next[fail[cur]][i];
Q.push(Next[cur][i]);
}
}
}
int query(char s[])
{
int len = strlen(s), cur = root, res = ;
for(int i = ;i < len;i++)
{
cur = Next[cur][s[i]-'a'];
int tmp = cur;
while (tmp != root)
{
res += val[tmp];
val[tmp] = ;
tmp = fail[tmp];
}
}
return res;
}
};
char s[N];
Tri AC;
int n;
int main()
{
scanf("%d",&n);
AC.init();
for(int i = ;i < n;i++)
{
scanf("%s",s);
AC.insert(s);
}
AC.build();
scanf("%s",s);
printf("%d\n",AC.query(s));
return ;
}

AC自动机板子(from. qwer)的更多相关文章

  1. AC自动机板子题/AC自动机学习笔记!

    想知道484每个萌新oier在最初知道AC自动机的时候都会理解为自动AC稽什么的,,,反正我记得我当初刚知道这个东西的时候,我以为是什么神仙东西,,,(好趴虽然确实是个对菜菜灵巧比较难理解的神仙知识点 ...

  2. hdu_2222_Keywords Search(AC自动机板子)

    题目连接:hdu_2222_Keywords Search 存个自己写的AC自动机 #include<cstdio> #include<cstring> #define F(i ...

  3. hdu2222 Keywords Search (AC自动机板子

    https://vjudge.net/problem/HDU-2222 题意:给几个模式串和一个文本串,问文本串中包含几个模式串. 思路:贴个板子不解释. #include<cstdio> ...

  4. Keywords Search HDU - 2222 AC自动机板子题

    In the modern time, Search engine came into the life of everybody like Google, Baidu, etc. Wiskey al ...

  5. ac自动机板子

    hdu2222 #include<bits/stdc++.h> #define ll long long #define M 500005 using namespace std; int ...

  6. 模板 AC自动机

    题目描述 有$N$ 个由小写字母组成的模式串以及一个文本串$T$ .每个模式串可能会在文本串中出现多次.你需要找出哪些模式串在文本串$T$ 中出现的次数最多. 输入输出格式 输入格式: 输入含多组数据 ...

  7. 洛谷P3966 单词 [TJOI2013] AC自动机

    正解:AC自动机 解题报告: 传送门! 先来提供一个40pts错解QAQ 首先看到这题就会想到AC自动机板子题2鸭!然后就照着那题的套路打一下,随便改一点儿,简单来说就是每次经过一个节点都要++,然后 ...

  8. P3808 【模板】AC自动机(简单版)

    题目背景 这是一道简单的AC自动机模板题. 用于检测正确性以及算法常数. 为了防止卡OJ,在保证正确的基础上只有两组数据,请不要恶意提交. 管理员提示:本题数据内有重复的单词,且重复单词应该计算多次, ...

  9. 「kuangbin带你飞」专题十七 AC自动机

    layout: post title: 「kuangbin带你飞」专题十七 AC自动机 author: "luowentaoaa" catalog: true tags: - ku ...

随机推荐

  1. 【tensorflow】tensorflow学习记录——安装、第一个程序篇

    机器学习,人工智能往后肯定是一个趋势,现阶段有必要研究一两个人工智能的工具,以免自己技术落伍,其中tensorflow就是一个很不错的项目,有谷歌开发后开源,下面开始学习安装和使用 安装篇: 很不幸, ...

  2. 详解单页面路由的几种实现原理(附demo)

    前言 路由是每个单页面网站必须要有的,所以,理解一下原理,我觉得还是比较重要的. 本篇,基本不会贴代码,只讲原理,代码在页底会有githup地址,主意,一定要放在服务本地服务器里跑(因为有ajax), ...

  3. Gemini.Workflow 双子工作流入门教程二:定义流程:流程节点介绍

    简介: Gemini.Workflow 双子工作流,是一套功能强大,使用简单的工作流,简称双子流,目前配套集成在Aries框架中. 下面介绍本篇教程:流程定义:流程节点属性. 流程节点: 左侧是节点工 ...

  4. 使用valgrind进行内存泄漏和非法内存操作检测

    valgrind是一个强大的工具,最常用的功能是用它来检测内存泄漏和非法内存的使用.要想让valgrind报告的更加细致,请使用-g进行编译. 基本命令如下: $ valgrind --tool=me ...

  5. java高级特性增强

    第4天 java高级特性增强 今天内容安排: 1.掌握多线程 2.掌握并发包下的队列 3.了解JMS 4.掌握JVM技术 5.掌握反射和动态代理 java多线程增强 .1. java多线程基本知识 . ...

  6. 使用C语言解析URL

    1. [代码]容易写成自己输入URL,这里测试一个例子     #include <stdio.h>#include <stdlib.h>#include <string ...

  7. eclipse的maven工程Dynamic Web Module 2.3 修改为3.0 解决办法

    1. 创建Maven Web工程 2. 项目只有src/main/resources >Java Build Path导入Tomcat运行环境 3. 删除以图片红框中的文件 4. Propert ...

  8. unity3d: how to display the obj behind the wall

    透墙显示,遮挡显示,使用ztest Tags { "Queue"="Overlay+1" "RenderType"="Transp ...

  9. leetcode 303. Range Sum Query - Immutable(前缀和)

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  10. 关于Socket 多线程 的一篇好文章

    http://www.kegel.com/c10k.html#topIt's time for web servers to handle ten thousand clients simultane ...