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. Apollo源码搭建调试看一文就够

    Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境.不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限.流程治理等特性,适用于微服务配置管理场景. 我 ...

  2. 来自AJPFX的二分法查找

    package com.heima.array; public class Demo2_Array { /**         * * A:案例演示                        * ...

  3. nvm安装nodejs

    1. 安装nvm 下载 nvm-windows解压缩 nvm-windows解压缩 nvm-setup双击运行 nvm-setup.exe选择next选择 [D:\dev][path1] 或 默认路径 ...

  4. colormap画出的图不是彩色问题

    针对matlab2017渲染出的彩色图是黑白的问题. t=labels; t(tstSet(:,end-))=Relabels; t=reshape(t,,); t=t'; figure imshow ...

  5. JWT认证阐述

    哥发达了,是时候实现一下儿时的梦想了,怡红院开起! 店面门脸装修如何?做生意,谁还没个镇店之宝啊?有请我的店长如花小姐! 没事哈!别怕,扭曲的五官往往都藏着一颗纯洁的心灵. 不管如何吧,我的怡红院算是 ...

  6. 用pycharm+django开发web项目

    pycharm是python的一个商业的集成开发工具,本人感觉做python开发还是很好用的,django是一个很流行的python web开源框架,本文就是使用pycharm+django来开发py ...

  7. sql发送邮件- html 格式

    ALTER PROCEDURE dbo.sx_pro_AutoEmailContent AS Begin declare @Rqty int declare @n int declare @m_rec ...

  8. JAVA——不简单的fianl关键字

    protected用来修饰 域,代表域的访问权限是:包权限 或者 不同包,但是是子类 : final 修饰常量只要是该常量代入的计算式,在编译时期,就会被执行计算,以减轻运行时的负担.(只对基本数据类 ...

  9. CSS3 自动旋转

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. clusterdb - 对一个PostgreSQL数据库进行建簇

    SYNOPSIS clusterdb [ connection-option...] [ --table | -t table] [ dbname] clusterdb [ connection-op ...