AC自动机模板
来源:互联网 发布:商家如何取消淘宝客 编辑:程序博客网 时间:2024/05/16 01:09
点击打开链接
在一个字符串中出现模板串的个数
#include <iostream>#include <string.h>#include <stdio.h>#include <algorithm>#include <queue>#include <math.h>#define MOD 1000000007using namespace std;const int N = 1e6+10;const int M = 26;struct Trie{ int next[N][M],end[N],fail[N]; int root,L; int newNode(){ for(int i = 0; i < M; ++i){ next[L][i] = -1; } end[L++] = 0; return L-1; } void init(){ L = 0; root = newNode(); } void insert(char buf[]){ int now = root; for(int i = 0; buf[i]; ++i){ int x = buf[i]-'a'; if(next[now][x] == -1) next[now][x] = newNode(); now = next[now][x]; } end[now]++; } void build(){ queue<int>Q; fail[root] = root; for(int i = 0; i < M; ++i){ if(next[root][i] == -1) next[root][i] = root; else{ fail[next[root][i]] = root; Q.push(next[root][i]); } } while(!Q.empty()){ int now = Q.front(); Q.pop(); for(int i = 0; i < M; ++i){ if(next[now][i] == -1) next[now][i] = next[fail[now]][i]; else{ fail[next[now][i]] = next[fail[now]][i]; Q.push(next[now][i]); } } } } int query(char buf[]){ int now = root; int res = 0; for(int i = 0; buf[i]; ++i){ now = next[now][buf[i]-'a']; int temp = now; while(temp != root){ res += end[temp]; end[temp] = 0; temp = fail[temp]; } } return res; }};char buf[N];Trie ac;int main(){ int T; int n; scanf("%d",&T); while(T--){ scanf("%d",&n); ac.init(); for(int i = 0; i < n; ++i){ scanf("%s",buf); ac.insert(buf); } ac.build(); scanf("%s",buf); printf("%d\n",ac.query(buf)); } return 0;}
0 0
- 【AC自动机】AC自动机模板
- AC自动机模板
- AC自动机模板
- AC自动机模板
- AC自动机模板
- AC自动机模板
- AC自动机模板
- AC自动机模板 hdu2222
- AC自动机模板
- AC自动机模板
- 数据结构--AC自动机--模板
- ac自动机模板
- AC自动机模板
- AC自动机模板
- hdu2222 ac自动机模板
- ac自动机模板。。。。。
- AC自动机模板 LA4670
- AC自动机模板
- C++ IplImage*的若干bug之二:cvReleaseImage问题
- HDU 4883 TIANKENG’s restaurant
- 光阴深处岁月静好可待流年
- 进程保活大全
- Linux下用文件IO的方式操作GPIO(/sys/class/gpio)
- AC自动机模板
- 二次函数的研究
- Mac 上安装 Office 2016 破解版
- 错误讯息:检索 COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件失败,原因是出现以下错误: 80070005 拒绝访问。 (异常来
- Spring 学习系列 -- Spring + Mybatis 从零开始配置多数据源访问
- 【985系列】985的方格难题
- POJ 1328Radar Installation
- poj 1152
- RXAndroid入门