AC自动机自我理解和模板
给出长度为m的文本
查询 n个单词出现的次数
用kmp 复杂度 n*m*(单词平均长度)
用字典树 复杂度 m*每次字典树遍历的平均深度)
AC自动机 复杂度 m (思路可以理解为kmp+字典树 )
正在学 代码没修改完
#include<bits/stdc++.h>
using namespace std;
;
;
struct node
{
node *fail;
node *next[kind];
int cnt;
node()
{
fail=NULL;
cnt=;
memset(next,NULL,sizeof(next));
}
}*q[maxn];
];
]; //模式串
int head,tail; //队列的头尾指针
void insert(char *str,node *root) // **************建树
{
node *p=root;
,index;
while(str[i])
{
index=str[i]-'a';
if(p->next[index]==NULL) p->next[index]=new node();
p=p->next[index];
i++;
}p->cnt++;
}
void build_ac_automation(node *root)
{
root->fail=NULL;
q[head++]=root;// tou zhi zheng
while(head!=tail)
{
node *temp=q[tail++];
node *p=NULL;
;i<;i++)
{
if(temp->next[i]!=NULL)
{
if(temp==root) temp->next[i]->fail=root;
else
{
p=temp->fail;
while(p!=NULL)
{
if(p->next[i]!=NULL)
{
temp->next[i]->fail=p->next[i];
break;
}
p=p->fail;
}
if(p==NULL) temp->next[i]->fail=root;
}
q[head++]=temp->next[i];
}
}
}
}
int ac_query(node *root)
{
;
;
int index;
int l=strlen(str);
node *p=root;
while(str[i])
{
index=str[i]-'a';
index=str[i]-'a';
while(p->next[index]==NULL && p!=root) p=p->fail;
p=p->next[index];
p=(p==NULL)?root:p;
node *temp=p;
)
{
num+=temp->cnt;
temp->cnt=-;
temp=temp->fail;
}
i++;
}
return num;
}
int32_t main()
{
}
banzi https://www.cnblogs.com/MingSD/p/8733762.html
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
const int INF = 0x3f3f3f3f;
;
typedef pair<int,int> pll;
, M = 1e3;
];
][];
];
];
;
void Insert(){
, len = strlen(str);
; i < len; i++){
int id = str[i] - 'a';
){
cnt[tot] = ;
fair[tot] = ;
trie[rt][id] = tot++;
}
rt = trie[rt][id];
}
cnt[rt]++;
}
void Build_tree(){
queue<int> q;
q.push();
int p;
while(!q.empty()){
int tmp = q.front();
q.pop();
; i < ; i++){
){
)
fair[trie[tmp][i]] = ;
else{
p = fair[tmp];
while(p){
if(trie[p][i]){
fair[trie[tmp][i]] = trie[p][i];
break;
}
else p = fair[p];
}
;
}
q.push(trie[tmp][i]);
}
}
}
}
int Query(){
, ret = , len = strlen(str);
; i < len; i++){
int id = str[i] - 'a';
) rt = fair[rt];
rt = trie[rt][id];
) rt = ;
int tmp = rt;
){
){
ret += cnt[tmp];
cnt[tmp] = -;
}
else break;
tmp = fair[tmp];
}
}
return ret;
}
void init(){
; i < tot; i++){
; j < ; j++)
trie[i][j] = ;
}
tot = ;
}
int main(){
int T;
scanf("%d", &T);
while(T--)
{
init();
int n;
scanf("%d", &n);
while(n--)
{
scanf("%s", str);
Insert();
}
Build_tree();
scanf("%s", str);
printf("%d\n", Query());
}
;
}
AC自动机自我理解和模板的更多相关文章
- AC自动机详解 (P3808 模板)
AC自动机笔记 0.0 前言 哇,好久之前就看了 KMP 和 Trie 树,但是似乎一直没看懂 AC自动机?? 今天灵光一闪,加上之前看到一些博客和视频,瞬间秒懂啊... 其实这个玩意还是蛮好理解的. ...
- HDOJ-2222(AC自动机+求有多少个模板串出现在文本串中)
Keywords Search HDOJ-2222 本文是AC自动机的模板题,主要是利用自动机求有多少个模板出现在文本串中 由于有多组输入,所以每组开始的时候需要正确的初始化,为了不出错 由于题目的要 ...
- KMP算法自我理解 和 模板
字符串 abcd abc abcd abc 匹配串 cdabcd 匹配串的 next 0 0 0 0 1 2: 开始匹配 abcd abc abcd abc cd abc d a,d 匹配失 ...
- 【AC自动机】HDU中模板题
[HDU2222] 最纯粹的裸题,错误点详见注释. #include<iostream> #include<cstdio> #include<cstring> #i ...
- ac自动机(tree+kmp模板)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
- 【题解】P3796【模板】AC自动机(加强版)
[题解]P3796 [模板]AC自动机(加强版) 记录当前\(cnt\)是第几个"星".记录第几个串是对应着第几个星. 这里补充一点对于\(AC\)自动机的理解.可能一直有个问题我 ...
- AC自动机--summer-work之我连模板题都做不出
这章对现在的我来说有点难,要是不写点东西,三天后怕是就一无所有了. 但写这个没有营养的blog的目的真的不是做题或提升,只是学习学习代码和理解一些概念. 现在对AC自动机的理解还十分浅薄,这里先贴上目 ...
- 洛谷--P3808 【模板】AC自动机(“假的“简单版)
如果你想要做出这道题,你需要先了解两个知识点: 1.字典树的构造 2.KMP算法(也就是fail指针的构造) 对于字典树,可以看看这个大佬: https://www.cnblogs.com/TheRo ...
随机推荐
- day_07_python_1124
01 昨日内容回顾 数据类型补充: str <---> list split join list <---> set set(list) list(set()) list &l ...
- 调用zabbix 分组api
调用zabbix 分组api,获取分组中主机host信息,并分类保存, #!/usr/bin/env python #coding:utf8 import requests import json i ...
- 【原创】<笔试题> 深圳市天软科技开发有限公司
时间:2018.03.03 上午 1.编写函数,实现字符串比较功能. 参考:http://blog.csdn.net/liubinzi123/article/details/8271683 /* * ...
- Java 几种showMessageDialog的表示
最近在做swing程序中遇到使用消息提示框的,JOptionPane类其中封装了很多的方法. 很方便的,于是就简单的整理了一下. 1.1 showMessageDialog 显示一个带有OK 按钮的模 ...
- 【阿圆实验】Grafana HA高可用方案
一.实现Grafana高可用 1.Grafana实现高可用性有两步: >>使用共享数据库存储仪表板,用户和其他持久数据>>决定如何存储会话数据. 2.Grafana高可用部署图 ...
- ORM版学员管理系统1
ORM版学员管理系统 班级表 表结构 class Class(models.Model): id = models.AutoField(primary_key=True) # 主键 cname = m ...
- Cracking The Coding Interview 1.3
//原文: // // Design an algorithm and write code to remove the duplicate characters in a string withou ...
- minifilter
暑假刚开始的时候,参照<寒江独钓>这本书,用VS2015写过的一个minifilter的框架,今天在博客上分享出来. VS2015已经有了minifilter的框架模板,直接生成了mini ...
- CentOS 查看文件大小 du -hs filename
du -hs [filename] 查看目录大小 [root@localhost opt]# 16M apache-tomcat- df -hv 查看整个磁盘使用状况 [root@rabbit66 ...
- 设置checkBox不拦截焦点
android:clickable="false"android:focusableInTouchMode="false"android:focusable=& ...