AC自动机妙用
理解题意之后,很自然的想到了用AC自动机搞,结果网上一搜,全是暴搜,按照自己的思想,AC自动机搞起,果然在提交了数次之后,看到了Accept。
AC自动机需要三个步骤:
第一步:建立字典树;
第二步:为字典树建立 Fail 指针 ;
第三步:进行匹配;
#include<iostream>
#include<string>
#include<string.h>
#include<cctype>
#include<queue>
#include<stdio.h> #define max(x,y) ((x) > (y) ? (x) : (y)) using namespace std ; struct Node {
Node *next[26] ;
Node *fail ;
bool is_over ;
int len ;
}; Node* new_node() {
Node *root = new Node ;
root ->fail = NULL ;
for(int i = 0 ; i< 26 ; i++)
root->next[i] = NULL ;
root ->is_over = false ;
return root ;
} void build_tree( Node *root , char *s ) {
int len = strlen(s) ;
for(int i = 0 ; i < len ; i++) {
if(root->next[s[i] - 'a'] == NULL)
root->next[s[i] - 'a'] = new_node() ;
root = root ->next[s[i] - 'a'] ;
}
root ->is_over = true ;
root ->len = len ;
} void build_fail(Node *root) {
Node *r = root ;
queue< Node* > q ;
q.push(root) ;
while(!q.empty()) {
r = q.front() ;
q.pop() ;
Node *p = r ;
for(int i = 0 ; i < 26 ; i++) {
if(p->next[i] != NULL) {
if(p == root) {
p->next[i]->fail = root ;
q.push(p->next[i]) ;
continue ;
}
while(r->fail->next[i] == NULL && r ->fail != root)
r = r->fail ;
if(r->fail ->next[i] != NULL)
p ->next[i]->fail = r ->fail ->next[i] ;
else
p ->next[i] ->fail = root ;
q.push(p->next[i]) ;
}
}
}
} int A_C(Node *root , char *s) {
int len = strlen(s) ;
Node *r = root ;
int count = 0 ;
for(int i = 0 ; i < len ; i++) {
if(!isalpha(s[i]))
continue ;
if(r->next[s[i]-'a'] != NULL) {
r = r ->next[s[i]-'a'] ;
if(r->is_over && !(islower(s[i+1])) && !(islower(s[i - r->len])) )
count++ ;
}
else {
while(r->next[s[i]-'a'] == NULL && r != root )
r = r ->fail ;
if(r ->next[s[i] - 'a'] != NULL)
r = r ->next[s[i] - 'a'] ;
else
r = root ;
if(r->is_over && (!(isalpha(s[i+1])) ) && !(islower(s[i - r->len])) )
count++ ;
}
} return count ;
} int main() {
int n , m ;
int t = 1 ;
while(scanf("%d%d",&n,&m) == 2) {
Node *root = new_node() ;
Node *r = root ;
while(n--) {
char s[30] ;
scanf("%s",&s) ;
getchar() ;
build_tree(root,s) ;
root = r ;
}
r->fail = NULL ;
root = r ;
build_fail(r) ;
char str[30][100] ;
char str1[30][100] ;
int count[30] = {0} ;
int ma = 0 ;
for(int i = 0 ; i < m ; i++) {
gets(str[i]) ;
strcpy(str1[i],str[i]) ;
int len = strlen(str[i]) ;
for(int j = 0 ; j < len ; j++)
if(isupper(str[i][j]))
str[i][j] += 32 ;
count[i] = A_C(root,str[i]) ;
ma = max(ma,count[i]) ;
}
cout << "Excuse Set #" << t++ << endl;
for(int k = 0 ; k < m ; k++)
if(count[k] == ma)
cout << str1[k] << endl ;
cout << endl ;
}
return 0 ;
}
AC自动机妙用的更多相关文章
- AC自动机讲解
今天花了半天肝下AC自动机,总算啃下一块硬骨头,熬夜把博客赶出来.. 正如许多博客所说,AC自动机看似很难很妙,而事实上不难,但的确很妙.笼统地说,AC自动机=Trie+KMP,但是仅仅知道这个并没有 ...
- Aho-Corasick automaton(AC自动机)解析及其在算法竞赛中的典型应用举例
摘要: 本文主要讲述了AC自动机的基本思想和实现原理,如何构造AC自动机,着重讲解AC自动机在算法竞赛中的一些典型应用. 什么是AC自动机? 如何构造一个AC自动机? AC自动机在算法竞赛中的典型应用 ...
- AC自动机——多个kmp匹配
(并不能自动AC) 介绍: Aho-Corasick automaton,最经典的处理多个模式串的匹配问题. 是kmp和字典树的结合. 精髓与灵魂: ①利用trie处理多个模式串 ②引入fail指针. ...
- AC自动机板子题/AC自动机学习笔记!
想知道484每个萌新oier在最初知道AC自动机的时候都会理解为自动AC稽什么的,,,反正我记得我当初刚知道这个东西的时候,我以为是什么神仙东西,,,(好趴虽然确实是个对菜菜灵巧比较难理解的神仙知识点 ...
- 洛谷 P3808 【模板】AC自动机(简单版) 题解
原题链接 前置知识: 字典树.(会 \(\texttt{KMP}\) 就更好) 显然呢,本题用 字典树 和 \(\texttt{KMP}\) 无法解决问题. 所以我们发明了一个东西: \(\textt ...
- KMP,HASH,Trie,AC自动机
我做个总结算了下午看了一下AC自动机和学习我的大生物(当然是多谢鑫神了)..完了要崩.. 1 KMP 只要是学过的人都觉得比较简单吧 但是学不会的人就感觉很难了,我是那种顿悟的然后感觉非常简单的人过程 ...
- 背单词(AC自动机+线段树+dp+dfs序)
G. 背单词 内存限制:256 MiB 时间限制:1000 ms 标准输入输出 题目类型:传统 评测方式:文本比较 题目描述 给定一张包含N个单词的表,每个单词有个价值W.要求从中选出一个子序列使 ...
- 【AC自动机】背单词
题意: 0 s v:添加价值为v的字符串s 1 t:查询t中含的s的权值和.(不停位置算多次) 思路: 在线AC自动机. 同学用过一个妙妙子的分块算法. 这里用二进制分组:通常用作把在线数据结构问题转 ...
- 基于trie树做一个ac自动机
基于trie树做一个ac自动机 #!/usr/bin/python # -*- coding: utf-8 -*- class Node: def __init__(self): self.value ...
随机推荐
- C语言入门(4)——常量、变量与赋值
对于基本数据类型量,按其取值是否可改变又分为常量和变量两种.在程序执行过程中,其值不发生改变的量称为常量,其值可变的量称为变量.它们可与数据类型结合起来分类. 常量 常量有字符常量(Character ...
- [Leetcode][Python]24: Swap Nodes in Pairs
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 24: Swap Nodes in Pairshttps://oj.leetc ...
- 【FZU】2152 文件系统
Problem 2152 文件系统 Accept: 63 Submit: 126 Time Limit: 1000 mSec Memory Limit : 32768 KB Probl ...
- lighttpd配置虚拟主机/php等WEB环境
lighttpd(1.4.37)配置如下 server.document-root = "/var/www/lighttpd/" server.port = 8888 server ...
- poj2728 Desert King --- 01分数规划 二分水果。。
这题数据量较大.普通的求MST是会超时的. d[i]=cost[i]-ans*dis[0][i] 据此二分. 但此题用Dinkelbach迭代更好 #include<cstdio> #in ...
- Linux下安装Oracle的过程和涉及的知识点-系列4
10.使用rpm安装包 假设本地有现成的相关包,能够直接使用rpm安装.rpm rpm包名,但有时会出现它须要其他包的支持,这时若须要忽略此提示.强行安装,运行rpm -i --force --nod ...
- 部署SharePoint2013解决方案
Add-SPSolutionInstall-SPSolution -Identity Grain2013.wsp -GACDeployment -CompatibilityLevel {14,15} ...
- OC 中的block使用
在iOS的开发过程中,使用块的地方很多也很方便,但是在使用块的过程中要注意内存泄露的问题. 在块创建的时候,会对块内的所有对象的引用计数加一,直到块销毁,所以在使用块的过程中需要我们进行处理,在这里以 ...
- C++/C#结构体转化-传string给C++
此例是把C#结构传给C++ C++: typedef struct VidyoClientInEventGroupChat_ { /*! Message (contents) to be sent t ...
- MongoDB入门学习(一)—— 安装和启动
最近由于工作需要,开始学习MongoDB数据库了.第一篇博文就从这里开始吧,以此记录下学习中的点点滴滴,为自己加油呢! (一) MongoDB简介 网上搜搜了一下:(来源:http://www.run ...