HDU 2222 Keywords search

Problem : 给若干个模式串,询问目标串中出现了多少个模式串。

**Solution : **复习了一下AC自动机。需要注意AC自动机中的fail,和next的含义。fail指向了一个最长的与当前匹配出具有相同后缀的一个前缀节点,next用来转移下一个字符,指向最远可以匹配的位置。另外,在目标串匹配时,需要用一个temp指针不断经过fail指针向前跳转,所有经过的节点都是符合的。

#include <iostream>
#include <cstring>
#include <queue>
using namespace std; const int N = 500008;
struct AC_Automan
{
int next[N][26];
int fail[N];
int cnt[N];
int root, tot;
int newnode()
{
for (int i = 0; i < 26; ++i) next[tot][i] = -1;
fail[tot] = -1;
cnt[tot] = 0;
return tot++;
}
void clear()
{
tot = 0;
root = newnode();
}
void insert(const string &s)
{
int p = root;
for (int i = 0, len = s.length(); i < len; ++i)
{
if (next[p][s[i] - 'a'] == -1) next[p][s[i] - 'a'] = newnode();
p = next[p][s[i] - 'a'];
}
++cnt[p];
}
void build()
{
queue <int> Q;
Q.push(root);
while (!Q.empty())
{
int p = Q.front(); Q.pop();
for (int i = 0; i < 26; ++i)
{
if (~next[p][i])
{
if (p == root) fail[next[p][i]] = root;
else fail[next[p][i]] = next[fail[p]][i];
Q.push(next[p][i]);
}
else
{
if (p == root) next[p][i] = root;
else next[p][i] = next[fail[p]][i];
}
}
}
}
int solve(const string &t)
{
int p = root, ans = 0;
for (int i = 0, len = t.length(); i < len; ++i)
{
p = next[p][t[i] - 'a'];
for (int temp = p; temp != root && cnt[temp] != -1; temp = fail[temp])
{
ans += cnt[temp];
cnt[temp] = -1;
}
}
return ans;
}
}ac; int main()
{
cin.sync_with_stdio(0);
int T; cin >> T;
for (int cas = 1; cas <= T; ++cas)
{
ac.clear();
int n; cin >> n;
for (int i = 0; i < n; ++i)
{
string s; cin >> s;
ac.insert(s);
}
ac.build();
string t; cin >> t;
cout << ac.solve(t) << endl;
}
}

HDU 2222 (AC自动机)的更多相关文章

  1. HDU 2222 AC自动机模板题

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...

  2. HDU 2222 ----AC自动机

    Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...

  3. HDU 2222 AC自动机 裸题

    题意: 问母串中出现多少个模式串 注意ac自动机的节点总数 #include <stdio.h> #include <string.h> #include <queue& ...

  4. HDU 2222 AC自动机模版题

    所学的AC自动机都源于斌哥和昀神的想法. 题意:求目标串中出现了几个模式串. 使用一个int型的end数组记录,查询一次. #include <cstdio> #include <c ...

  5. hdu 2222(AC自动机模版题)

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

  6. hdu 2222 ac自动机更新模板 for onSite contest

    http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 #include <cstdio> #include <cstdlib> ...

  7. HDU 2222 AC自动机(模版题)

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

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

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

  9. HDU 2222 & ac自动机模板

    题意: 求n个模板串在匹配串中出现了几个. SOL: 反正就是模板啦...似乎比KMP都简单----这么说似乎有点不道德...毕竟先看的KMP而他们并没有什么不同... 貌似自己的理解和他们画的图还是 ...

随机推荐

  1. 每天学点linux命令之locate 与 find 命令

    定位某个文件的位置 方法一 sudo find / -name libGLEW.so.1.13 -type f 方法二 sudo updatedb && locate libopenc ...

  2. Java实现三角形计数

    题: 解: 这道题考的是穷举的算法. 一开始看到这道题的时候,本能的想到用递归实现.但使用递归的话数据少没问题,数据多了之后会抛栈溢出的异常.我查了一下,原因是使用递归创建了太多的变量, 每个变量创建 ...

  3. "言官误国"

    "言官误国" 之前读<明朝那些事儿>的时候,了解到了一个全新的概念,确切的说与之前的印象完全不符合的概念:言官.之前我印象中的言官都是魏征那样的人,为国为民.冒死直谏等 ...

  4. Spring------IOC&DI

    一.Spring? Spring兴起:2003年,由Rod Johnson创建.总的来说,Spring Framwork从它诞生至今都一直为人所称道,它的伟大之处自此可见一斑. 核心:IOC& ...

  5. Elasticsearch--集群&吞吐量

    目录 高查询和高吞吐量 过滤器缓存 字段数据缓存和断路器 断路器 存储模块 索引缓冲和刷新率 索引刷新率 线程池的配置 一些通用的建议来配置高索引和查询吞吐量的集群 高查询和高吞吐量 过滤器缓存 过滤 ...

  6. Vue 拦截器的使用

    拦截器 可以全局进行拦截器设置.拦截器在发送请求前或响应返回时做一些特殊的处理. 拦截器的注册 Vue.http.interceptors.push({ request: function ( req ...

  7. jpa,querydsl

    [TOC] # jpa ## 生成通用模板 实现自定义方法有两种方法: 1. 根据衍生规则进行实现,此种情况简单:查询方法衍生规则 http://docs.spring.io/spring-data/ ...

  8. Ubuntu 下更新或下载输入法(搜狗)

    ubuntu12.04的fcitx版本不支持,不满足依赖,需要更新fcitx 添加fcitx源添加fcitx源命令 : sudo add-apt-repository ppa:fcitx-team/n ...

  9. springMVC返回Base64位编码图片验证码

    import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Graphics2D;impor ...

  10. Vue + Django 2.0.6 学习笔记 6.1-6.2 商品类别数据接口

    这两节主要是说获取商品类别的1 2 3类的列表和某个类的详情 我直接上代码吧 views.py: from .serializers import CategorySerializer class C ...