Problem's Link

Mean:

有n个模式串和一篇文章,统计有多少模式串在文章中出现(正反统计两次).

analyse:

好久没写AC自动机了,回顾一下AC自动机的知识。

本题在构造文章的时候需要仔细一点,其他没什么Trick,和普通AC自动机做法一样:

build Trie  --->  build Fail_Ptr ---> matching_and_count

Time complexity: O(N*L+M)

Source code: 

/*
* this code is made by crazyacking
* Verdict: Accepted
* Submission Date: 2015-07-19-10.29
* Time: 0MS
* Memory: 137KB
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#define  LL long long
#define  ULL unsigned long long
using namespace std;const int M = 6000005;
class node {
public:bool flag;node *fail, *next[26];node() {flag = false;fail = NULL;memset( next, NULL, sizeof next );}
};
node *root;
queue<node*> q;
char s[M], str[M];void Insert( char *str ) { // build Trie-Treenode *p = root;int i = 0, index;while( str[i] ) {index = str[i] - 'A';if( p->next[index] == NULL )p->next[index] = new node();p = p->next[index];++i;}p->flag = true;
}void build_ac_automation( node *root ) { // build fail ptrroot->fail = NULL;while( !q.empty() ) q.pop();q.push( root );while( !q.empty() ) {node *temp = q.front();q.pop();node *p = NULL;for( int i = 0; i < 26; ++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.push( temp->next[i] );}}}
}int query( node *root ) { // mathing and countnode *p = root;int i = 0, ans = 0, index;while( str[i] ) {index = str[i] - 'A';while( p->next[index] == NULL && p != root )p = p->fail;p = p->next[index];if( p == NULL )p = root;node *temp = p;while( temp != root && temp->flag ) {ans++;temp->flag = false;temp = temp->fail;}i++;}return ans;
}inline void build_str( char *s ) {int len = strlen( s ), cnt = -1;for( int i = 0; i < len; ++i ) {if( s[i] >= 'A' && s[i] <= 'Z' ) {str[++cnt] = s[i];continue;}if( s[i] == '[' ) {++i;int num = 0;for( ; s[i] >= '0' && s[i] <= '9'; ++i ) {num = num * 10 + ( s[i] - '0' );}char ch = s[i];++i;for( int j = 1; j <= num; ++j )str[++cnt] = ch;}}str[++cnt] = '\0';
}int main() {ios_base::sync_with_stdio( false );cin.tie( 0 );int Cas;scanf( "%d", &Cas );while( Cas-- ) {root = new node();int n;scanf( "%d", &n );while( n-- ) {scanf( "%s", s );Insert( s );}build_ac_automation( root );scanf( "%s", s );build_str( s );int ans = query( root );strrev( str );ans += query( root );printf( "%d\n", ans );}return 0;
}

转载于:https://www.cnblogs.com/crazyacking/p/4653984.html

AC自动机 - 多模式串的匹配 --- HDU 3695 Computer Virus on Planet Pandora相关推荐

  1. HDU 3695 Computer Virus on Planet Pandora (AC自己主动机)

    题意:有n种病毒序列(字符串),一个模式串,问这个字符串包括几种病毒. 包括相反的病毒也算.字符串中[qx]表示有q个x字符.具体见案列. 0 < q <= 5,000,000尽然不会超, ...

  2. AC自动机 + 概率dp + 高斯消元 --- HDU 5955 or 2016年沈阳icpc H [AC自动机 + 概率dp + 高斯消元]详解

    题目链接 题目大意: 就是有NNN个人,每个人都会猜一个长度为LLL的只包含{1,2,3,4,5,6}\{1,2,3,4,5,6\}{1,2,3,4,5,6}的序列,现在裁判开始投掷骰子,并且把每次的 ...

  3. HDU - 3341 Lost's revenge(AC自动机+状压dp)

    题目链接:点击查看 题目大意:给出 n 个模式串,最后给出一个匹配串,问如何重新排列匹配串,可以使得匹配串尽可能多的匹配模式串 题目分析:因为是模式串和匹配串的匹配,所以考虑AC自动机,因为数据范围比 ...

  4. HDU - 2457 DNA repair(AC自动机+dp)

    题目链接:点击查看 题目大意:给出 n 个匹配串,再给出一个模式串,问最少修改模式串中多少个字母可以使得模式串中不含有任意一个匹配串 题目分析:因为又是模式串与匹配串的题目,虽然与一般意义上的匹配不太 ...

  5. hdu 2222 Keywords Search(ac自动机)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给你一系列子串,再给你一个主串问你主串一共有几个匹配子串 原来使用字典树写的但数据有点大T ...

  6. AC自动机:多模式串匹配实现敏感词过滤

    文章出处:极客时间<数据结构和算法之美>-作者:王争.该系列文章是本人的学习笔记. 1 敏感词过滤场景 在很多支持用户发表内容的网站,都有敏感词过滤替换的功能.例如将一些淫秽.反动内容过滤 ...

  7. AC自动机——多个kmp匹配

    (并不能自动AC) 介绍: Aho-Corasick automaton,最经典的处理多个模式串的匹配问题. 是kmp和字典树的结合. 精髓与灵魂: ①利用trie处理多个模式串 ②引入fail指针. ...

  8. ac自动机 匹配最长前缀_AC自动机算法

    AC自动机简介: 首先简要介绍一下AC自动机:Aho-Corasick automation,该算法在1975年产生于贝尔实验室,是著名的多模匹配算法之一.一个常见的例子就是给出n个单词,再给出一段包 ...

  9. HDU - 2896 病毒侵袭(AC自动机)

    题目链接:点击查看 题目大意:给出 n 个模式串,再给出 m 个匹配串,问有多少个模式串在匹配串中出现,需要分别对应上其编号 题目分析:对应编号问题我们可以直接开一个数组映射,也是比较经典的模板问题了 ...

最新文章

  1. 用加法器构造能够实现连续加法的电路
  2. StarUML启动报RPC服务器不可用错误
  3. C#对象序列化与反序列化zz
  4. 解析字符串获取路径_node学习--path 路径模块
  5. LightOJ 1410 Consistent Verdicts(找规律)
  6. 手机html在哪个文件里,手机录像在哪个文件夹
  7. 蒟蒻的代码规范与文档编写规范
  8. js实现抽饭系统(类似抽检系统)双按钮控制系统
  9. C4D预设如何安装?
  10. 机器学习入门(1、特征抽取)
  11. mysql严格区分大小写吗_MySQL是否区分大小写
  12. jQuery 插件——免费版
  13. 安装ruby1.9.3-p0及redmon来监控redis
  14. 猫哥教你写爬虫 045--协程
  15. 计算机知识小口诀,字根表口诀怎么快速背-小学数学:一年级20以内加减法口诀表,附背诵技巧!...
  16. Gradle Task的使用
  17. 乱花渐欲迷人眼的C编译器中,谁才是“编译之王”?
  18. 幸运转盘中的芯片——CD4017和NE555
  19. rdkafka线程过多_我是如何处理大并发量订单处理的 KafKa部署总结
  20. Locust的学习笔记(一、环境搭建以及初识locust)

热门文章

  1. Modularity(模块化-UMD通用模式)
  2. 7-81 编程团体赛 (20 分)
  3. 7-2 数组元素循环右移问题 (40 分)
  4. Hibernate之二级缓存
  5. CCF CSP 201809-1 卖菜
  6. UVA - 820 Internet Bandwidth(最大流模板题)
  7. Homework 1_SQL Server中由于外键约束而删除数据失败
  8. Hive ROW_NUMBER,RANK(),DENSE_RANK()
  9. 变量在函数内外的作用域 3
  10. LeetCode:Balanced Binary Tree