HDU 2222 (AC自动机)
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自动机)的更多相关文章
- HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
- HDU 2222 ----AC自动机
Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...
- HDU 2222 AC自动机 裸题
题意: 问母串中出现多少个模式串 注意ac自动机的节点总数 #include <stdio.h> #include <string.h> #include <queue& ...
- HDU 2222 AC自动机模版题
所学的AC自动机都源于斌哥和昀神的想法. 题意:求目标串中出现了几个模式串. 使用一个int型的end数组记录,查询一次. #include <cstdio> #include <c ...
- hdu 2222(AC自动机模版题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- hdu 2222 ac自动机更新模板 for onSite contest
http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 #include <cstdio> #include <cstdlib> ...
- HDU 2222 AC自动机(模版题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- Keywords Search HDU - 2222 AC自动机板子题
In the modern time, Search engine came into the life of everybody like Google, Baidu, etc. Wiskey al ...
- HDU 2222 & ac自动机模板
题意: 求n个模板串在匹配串中出现了几个. SOL: 反正就是模板啦...似乎比KMP都简单----这么说似乎有点不道德...毕竟先看的KMP而他们并没有什么不同... 貌似自己的理解和他们画的图还是 ...
随机推荐
- android开发学习——This support library should not use a different version
http://blog.csdn.net/mvpstevenlin/article/details/55209966 完美解决系列
- ViewPager讲解以及ViewFlipper
1.加入ViewPager最好导入<android.support.v4.view.ViewPager>兼容低版本 2.将布局转换为View的方法 3.适配器类型 PagerAdapter ...
- OpenGl之旅-—如何使用code blocks创建一个opengl项目
开始学习opengl啦,练习用的编辑器是code blocks. 首先当然是要清楚如何使用code blocks创建一个opengl项目了. 首先必须先引用opengl的库glut,网上百度下载一个完 ...
- Websocket 关闭浏览器报错
这个报错,是因为你关闭之后,websocket 自动连接失败造成的 只要在你的websocket 运行的类里面加上: @OnError public void onError(Throwable e, ...
- 开源一个一个NodeJS 代理服务器扫描工具,可以用来***
鉴于我朝很多网站访问不了,google等就是大悲剧,之前一直在用VPN,但是公司内网VPN被封,诸多工具也惨遭毒手..我辈怎能容忍. 目前只有代理没有被封,于是搞了个代理扫描工具并开源: https: ...
- maven多模块项目,多web合并项目使用心得
Fixflow,做中国最好的开源流程引擎!项目地址https://github.com/fixteam/fixflow 此文章适合maven初学者或想接触maven的用户,讲的只是皮毛,高手请自觉略过 ...
- 【java】查重类的实现
import java.util.Vector; public class ElementCheck { // 重复优先 static Vector<Integer> CheckSameE ...
- MSYS2 使用
在Windows下编译mongo-c-driver 1.3.x 在Windows下编译mongo-c-driver 1.3.x 1.安装 MSYS2https://sourceforge.net/pr ...
- 20181228 模拟赛 T3 字符串游戏 strGame 博弈论 字符串
3 字符串游戏(strGame.c/cpp/pas) 3.1 题目描述 pure 和 dirty 决定玩 T 局游戏.对于每一局游戏,有n个字符串,并且每一局游戏由K轮组成.具体规则如下:在每一轮 ...
- [Python3网络爬虫开发实战] 1.3.2-Beautiful Soup的安装
Beautiful Soup是Python的一个HTML或XML的解析库,我们可以用它来方便地从网页中提取数据.它拥有强大的API和多样的解析方式,本节就来了解下它的安装方式. 1. 相关链接 官方文 ...