https://vjudge.net/problem/HDU-2222

题意:给几个模式串和一个文本串,问文本串中包含几个模式串。

思路:贴个板子不解释。

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int N=26;
const int MAXN=500005;
struct Trie{
int next[MAXN][N],fail[MAXN],end[MAXN];
int root;
int tot;
int newnode()
{
for(int i=0;i<N;i++)
next[tot][i]=-1;
end[tot++]=0;
return tot-1;
}
void init()
{
tot=0;
root=newnode();
} void insert(char buf[])
{
int len=strlen(buf);
int now=root;
for(int i=0;i<len;i++)
{
int k=buf[i]-'a';
if(next[now][k]==-1)
next[now][k]=newnode();
now=next[now][k];
}
end[now]++;
} void build()
{
queue<int> que;
fail[root]=root;
for(int i=0;i<N;i++)
if(next[root][i]==-1)
next[root][i]=root;
else
{
fail[next[root][i]]=root;
que.push(next[root][i]);
} while(!que.empty())
{
int now = que.front();
que.pop();
for(int i=0;i<N;i++)
if(next[now][i]==-1)
next[now][i]=next[fail[now]][i];
else
{
fail[next[now][i]]=next[fail[now]][i];
que.push(next[now][i]);
}
}
} int query(char buf[])
{
int len=strlen(buf);
int now=root;
int res=0;
for(int i=0;i<len;i++)
{
now=next[now][buf[i]-'a'];
int temp=now;
while(temp!=root)
{
res+=end[temp];
end[temp]=0;//模式串只在主串中匹配一次就可以了
temp=fail[temp];
}
}
return res;
} };
Trie ac;
char buf[MAXN+MAXN];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
ac.init();
for(int i=0;i<n;i++)
{
scanf("%s",buf);
ac.insert(buf);
}
ac.build();
scanf("%s",buf);
printf("%d\n",ac.query(buf));
} return 0;
}

hdu2222 Keywords Search (AC自动机板子的更多相关文章

  1. hdu2222 Keywords Search ac自动机

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 题目: Keywords Search Time Limit: 2000/1000 MS ...

  2. HDU2222 Keywords Search [AC自动机模板]

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  3. HDU2222 Keywords Search —— AC自动机

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 Keywords Search Time Limit: 2000/1000 MS (Java/O ...

  4. hdu2222 KeyWords Search AC自动机入门题

    /** 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:题意:给定N(N <= 10000)个长度不大于50的模式串,再给定一个长度为L ...

  5. HDU2222 Keywords Search ac自动机第一题

    指针我一般都会出错,所以还是自己写数组版本. In the modern time, Search engine came into the life of everybody like Google ...

  6. 【HDU2222】Keywords Search AC自动机

    [HDU2222]Keywords Search Problem Description In the modern time, Search engine came into the life of ...

  7. [hdu2222] [AC自动机模板] Keywords Search [AC自动机]

    AC自动机模板,注意!ch,Fail,lab数组的大小不是n而是节点个数,需要认真计算! #include <iostream> #include <algorithm> #i ...

  8. Keywords Search(AC自动机模板)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  9. Keywords Search AC自动机

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

随机推荐

  1. 【iOS】copy 关键字

    以前没注意过 iOS 的 copy, nonatomic, assign, weak, strong 等关键字. 偏偏今天遇到了一个问题,恰恰是关键字的问题,如图: 之前用的是 assign, 没有用 ...

  2. Apache ActiveMQ 实践 <一>

    一.下载最新版本 ActiveMq http://activemq.apache.org/activemq-5152-release.html,下载目录如下: 二.创建项目 1.普通项目 添加 jar ...

  3. 配置多个JDK存在的问题与解决方案 (亲测可用)

    安装多个JDK时的技巧 (亲测可用) 我的电脑本来是JDK8的,后来的想在不同的JDK版本下测试JDK的垃圾回收器. 一开始的的思路是,先安装JDK,为每个JDK配置自己的家目录,然后在想用哪个版本的 ...

  4. 【简洁易懂】CF372C Watching Fireworks is Fun dp + 单调队列优化 dp优化 ACM codeforces

    题目大意 一条街道有$n$个区域. 从左到右编号为$1$到$n$. 相邻区域之间的距离为$1$. 在节日期间,有$m$次烟花要燃放. 第$i$次烟花燃放区域为$a_i$ ,幸福属性为$b_i$,时间为 ...

  5. vscode c 语言 win10

    在看 CSAPP 一些课程,一些c 语言的小程序的例子,想跑起来试试,用一个DEV  c++  简单上手,但这是一个上古的IDE, 前端开发中的代码不全,语法高亮,都不太好,就想着为什么不折腾一下 V ...

  6. org.apache.spark.logging类报错

    一,1 在使用spark读取kafka数据时,当spark升级到2.0之后,出现如上问题:之前遇到了,当时在工程里面添加了org.apache.spark.Logging类,能够运行. 但是在后期使用 ...

  7. 动态SQL查询

    if+where: 用于查询操作,where标签可以智能判断是否添加and.or.where关键词 示例: <select id="findByParam" resultTy ...

  8. Spring 2017 Assignments1

    一.作业要求 原版:http://cs231n.github.io/assignments2017/assignment1/ 翻译:http://www.mooc.ai/course/268/lear ...

  9. GitPage部署

    前言 该文章主要为了记录我如何在GitPage上面部署博客网站,这里的话,码云上面也有相同的功能. 若有小伙伴担心GitHub担心把中国的访问也禁了的话(大概不会吧),可以在码云上面部署.流程应该是差 ...

  10. Sublime Text 3 使用手册

    Ctrl+Shift+P:打开命令面板 Ctrl+P:搜索项目中的文件 Ctrl+G:跳转到第几行 Ctrl+W:关闭当前打开文件 Ctrl+Shift+W:关闭所有打开文件 Ctrl+Shift+V ...