hdu 2222 Keywords Search ac自动机模板
题目链接
先整理一发ac自动机模板..
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <complex>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef complex <double> cmx;
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int mod = 1e9+7;
const int inf = 1061109567;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
const int maxn = 5e5+5;
struct AC
{
int nxt[maxn][26], fail[maxn], end[maxn];
int root, len;
int node() {
mem1(nxt[len]);
end[len++] = 0;
return len-1;
}
void init() {
len = 0;
root = node();
}
void insert(char str[]) {
int l = strlen(str);
int now = root;
for(int i = 0; i < l; i++) {
if(nxt[now][str[i]-'a'] == -1) {
nxt[now][str[i]-'a'] = node();
}
now = nxt[now][str[i]-'a'];
}
end[now]++;
}
void build() {
queue <int> q;
fail[root] = root;
for(int i = 0; i < 26; i++) {
if(nxt[root][i] == -1) {
nxt[root][i] = root;
} else {
fail[nxt[root][i]] = root;
q.push(nxt[root][i]);
}
}
while(!q.empty()) {
int now = q.front(); q.pop();
for(int i = 0; i < 26; i++) {
if(nxt[now][i] == -1) {
nxt[now][i] = nxt[fail[now]][i];
} else {
fail[nxt[now][i]] = nxt[fail[now]][i];
q.push(nxt[now][i]);
}
}
}
}
int query(char str[]) {
int l = strlen(str);
int now = root, ans = 0;
for(int i = 0; i < l; i ++) {
now = nxt[now][str[i]-'a'];
int tmp = now;
while(tmp != root) {
ans += end[tmp];
end[tmp] = 0;
tmp = fail[tmp];
}
}
return ans;
}
}ac;
char s[1000005];
int main()
{
int t, n;
cin>>t;
while(t--) {
ac.init();
scanf("%d", &n);
for(int i = 0; i < n; i++) {
scanf("%s", s);
ac.insert(s);
}
ac.build();
scanf("%s", s);
printf("%d\n", ac.query(s));
}
return 0;
}
hdu 2222 Keywords Search ac自动机模板的更多相关文章
- HDU 2222 Keywords Search(AC自动机模板题)
学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...
- HDU 2222 Keywords Search (AC自动机)(模板题)
<题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...
- hdu 2222 Keywords Search——AC自动机
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2222 第一道AC自动机! T了无数边后终于知道原来它是把若干询问串建一个自动机,把模式串放在上面跑:而且只 ...
- hdu 2222 Keywords Search ac自动机入门
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...
- HDU 2222 Keywords Search (AC自动机)
题意:就是求目标串中出现了几个模式串. 思路:用int型的end数组记录出现,AC自动机即可. #include<iostream> #include<cstdio> #inc ...
- Match:Keywords Search(AC自动机模板)(HDU 2222)
多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...
- POJ2222 Keywords Search AC自动机模板
http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数 ...
- Keywords Search(AC自动机模板)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- hdu 2222 Keywords Search - Aho-Corasick自动机
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...
随机推荐
- JSP 最佳实践: 用 jsp:include 控制动态内容
在新的 JSP 最佳实践系列的前一篇文章中,您了解了如何使用 JSP include 伪指令将诸如页眉.页脚和导航组件之类的静态内容包含到 Web 页面中.和服务器端包含一样,JSP include ...
- 网站建设之Django搭建与配置
总是忘记一些问题解决的细节,终于发现做笔记的必要了.一步一步慢慢写,慢慢积累吧.从开始接触计算机,从硬件到系统到软件,遇到的问题真心不算少了,都记下来的话也得有本书厚了. Linux Version: ...
- Android WebView 小结
1.设置网页的缩放比例的时候,可以使用以下属性: mWebview.setInitialScale(25);//为25%,最小缩放等级 WebSettings setting = mWebview.g ...
- javascript 获取元素宽高
style.width,clientWidth,offsetWidth <!doctype html> <html> <head> <meta charset ...
- mysql性能优化学习笔记(1)优化目的、方向及数据库准备
前言: 最近参加面试,问到了很多关于mysql的优化方面的问题,回答的不是很好,也是因为原先做的项目流量不是很大,所以对mysql优化不是太了解,所以趁着周末,恶补一下. 本文来源于慕课网sqlerc ...
- OpenCV2.4.9+VS2012安装与配置
需要下载并安装Visual Studio 2012 然后在OpenCV官网下载安装OpenCV2.4.9 for Windows,网址为http://opencv.org/downloads.html ...
- Java之线程的生命周期
在Java中,线程有5中不同状态,分别是:新建(New).就绪(Runable).运行(Running).阻塞(Blocked)和死亡(Dead).它们之间的转换图如下: 上图有一个例外,调用yiel ...
- [Backbone.js]如何用backbone写一个仿网页版微信的webapp?
var Chat = Backbone.Model.extend({ idAttribute:'id', initialize:function(options){ var users = this. ...
- python---连接MySQL第四页
python缓存结果集式的cursor可以用来提高性能. 例子: #!conding:utf-8 from mysql.connector import errorcode import mysql. ...
- 使用SndPlaySound从内存中播放WAV
有些时候,我们写的程序需要播放音乐,一般大家会采用以下几种方式:1.把音乐文件放置在某个特定的目录,例如Sound,WAV,Music供程序读取播放:2.使用资源来存放音乐.有些人估计也是使用这个方法 ...