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 = ;
class node {
public:
bool flag;
node *fail, *next[];
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-Tree
node *p = root;
int i = , 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 ptr
root->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 = ; 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.push( temp->next[i] );
}
}
}
} int query( node *root ) { // mathing and count
node *p = root;
int i = , ans = , 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 = -;
for( int i = ; i < len; ++i ) {
if( s[i] >= 'A' && s[i] <= 'Z' ) {
str[++cnt] = s[i];
continue;
}
if( s[i] == '[' ) {
++i;
int num = ;
for( ; s[i] >= '' && s[i] <= ''; ++i ) {
num = num * + ( s[i] - '' );
}
char ch = s[i];
++i;
for( int j = ; j <= num; ++j )
str[++cnt] = ch;
}
}
str[++cnt] = '\0';
} int main() {
ios_base::sync_with_stdio( false );
cin.tie( );
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 ;
}

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

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

    题目连接:hdu 3695 Computer Virus on Planet Pandora 题目大意:给定一些病毒串,要求推断说给定串中包括几个病毒串,包括反转. 解题思路:将给定的字符串展开,然后 ...

  2. hdu 3695:Computer Virus on Planet Pandora(AC自动机,入门题)

    Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 256000/1280 ...

  3. HDU 3695 Computer Virus on Planet Pandora(AC自动机模版题)

    Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 256000/1280 ...

  4. hdu ----3695 Computer Virus on Planet Pandora (ac自动机)

    Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 256000/1280 ...

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

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

  6. AC自动机 - 多模式串的匹配运用 --- HDU 3065

    病毒侵袭持续中 Problem's Link:http://acm.hdu.edu.cn/showproblem.php?pid=3065 Mean: 略 analyse: AC自动机的运用. 这一题 ...

  7. AC自动机 - 多模式串的匹配运用 --- HDU 2896

    病毒侵袭 Problem's Link:http://acm.hdu.edu.cn/showproblem.php?pid=2896 Mean: 略 analyse: AC自动机的运用,多模式串匹配. ...

  8. hdu 3695 10 福州 现场 F - Computer Virus on Planet Pandora 暴力 ac自动机 难度:1

    F - Computer Virus on Planet Pandora Time Limit:2000MS     Memory Limit:128000KB     64bit IO Format ...

  9. HDU 3695 / POJ 3987 Computer Virus on Planet Pandora

      Computer Virus on Planet Pandora Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1353 ...

随机推荐

  1. apache 80端口部属多站点配置

    1.在httpd.conf文件里启用虚拟主机功能,即去掉下面配置项前面的# #LoadModule vhost_alias_module modules/mod_vhost_alias.so 2..在 ...

  2. JavaScript 调试小技巧

    'debugger;' 除了console.log,debugger就是另一个我很喜欢的快速调试的工具,将debugger加入代码之后,Chrome会自动在插入它的地方停止,很像C或者Java里面打断 ...

  3. IOS开发 应用程序图标数字角标

    其实实现这个功能很简单,只要调用UIApplication即可.   用法用例:[UIApplication sharedApplication].applicationIconBadgeNumber ...

  4. ubuntu下取代ping的好工具tcpping

    $ sudo apt-get install tcptraceroute bc$ cd /usr/bin$ sudo wget http://www.vdberg.org/~richard/tcppi ...

  5. 未能正确加载“Microsoft.VisualStudio.Editor.Implementation.EditorPackage”包。

    最近在升级 Visual Studio 2015 Update 3 的过程中,等了很长时间都没一点进展,于是就强行终止了升级程序,但VS也因此出了问题. 后来经过修复,不行,卸载再重装,仍然提示这个错 ...

  6. python-推荐

    users={"Angelica": {"Blues Traveler": 3.5, "Broken Bells": 2.0, " ...

  7. SQL语句转摘

    http://www.cnblogs.com/Olive116/p/3271706.html 收藏没有用,来收到留链接

  8. viewWithTag获取subview规则详解

    通常我们在view层级里面对subView的操作可以通过两种方式:1.保留一个subview的引用,然后在类中通过该引用对该subview进行操作,但是要注意在适当的位置添加内存维护的代码,退出前手动 ...

  9. CentOS 7 上编译安装MySQL 5.6.23

    1.下载源码 wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.23.tar.gz 2.解压 tar zxvf mysql-5.6 ...

  10. 读书笔记_Effective_C++_条款四十一:了解隐式接口和编译期多态

    从本条款开始,就进入了全书的第七部分:模板与泛型编程.模板与泛型在C++中是非常重要的部分,还记得本书第一章时,把C++视为一个联邦,它由四个州政府组成,其中一个政府就是模板与泛型了. 本条款是一个介 ...