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. 数据流和ByteArray

    问题:如何把一个long类型的数写进一个文件里 所以现在有DataInputStream和DataOutputStream 这两个是节点流 例子代码: import java.io.*; public ...

  2. selenium通过autoit实现上传和下载

    autoit安装目录如下: AutoIt Windows Info   用于帮助我们识Windows控件信息. Compile Script to.exe 用于将AutoIt生成 exe 执行文件. ...

  3. vue.js学习参考手册

    参考手册 示例:www.51siyuan.cn/161.html

  4. LN : leetcode 646 Maximum Length of Pair Chain

    lc 646 Maximum Length of Pair Chain 646 Maximum Length of Pair Chain You are given n pairs of number ...

  5. git创建分支及日常使用

    克隆代码 git clone https://github.com/master-dev.git 查看所有分支 git branch --all # 默认只有master分支,所以会看到如下两个分支 ...

  6. Ubuntu Linux14 64位下在Android studio下用gradle编译Andrid项目时发生libz.so.1共享库找不到的解决方法。

    ---恢复内容开始--- 我在Ubuntu14 64为下安装了AS,但在用Gradle编译项目时总是报找不到 libz.so.1的错误. error while loading shared libr ...

  7. Caffe结构

    caffe可以分为3层结构:blob,layer,net 在layer中,input data用bottom表示,output data用top表示.每一个layer定义了三种操作,setup(Lay ...

  8. eclipse包分层

    方法很简单,如下图所示: 1.点击项目栏窗口的右上角的倒三角 2.选择Pachage Presentation(包呈现) 3.选择Hierarchical(分层)

  9. ICMP协议和ping命令

    当网络不通的情况下,通常会想到ping命令,ping一下,但是ping命令内部如何执行的,可能并不清楚,其实ping是基于ICMP协议进行工作的.  一.ICMP协议的格式 ICMP是在RFC 792 ...

  10. Android图像处理之Bitmap类(1)

    Bitmap是Android系统中的图像处理的最重要类之一.用它可以获取图像文件信息,进行图像剪切.旋转.缩放等操作,并可以指定格式保存图像文件.本文从应用的角度,着重介绍怎么用Bitmap来实现这些 ...