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. GitHub 优秀的 Android 开源项目(转)

    今天查找资源时看到的一篇文章,总结了很多实用资源,十分感谢原作者分享. 转自:http://blog.csdn.net/shulianghan/article/details/18046021 主要介 ...

  2. 一个类有两个方法,其中一个是同步的,另一个是非同步的; 现在又两个线程A和B,请问:当线程A访问此类的同步方法时,线程B是否能访问此类的非同步方法?

    一个类有两个方法,其中一个是同步的,另一个是非同步的:现在又两个线程A和B,请问:当线程A访问此类的同步方法时,线程B是否能访问此类的非同步方法? 答案:可以 验证 package com.my.te ...

  3. SSTable and Log Structured Storage: LevelDB

    If Protocol Buffers is the lingua franca of individual data record at Google, then the Sorted String ...

  4. dapper 操作类封装

    using System; using System.Collections.Generic; using System.Data; using System.Data.SQLite; using S ...

  5. emoji和utf8mb4字符集

    mysql 的 utf8 不支持 emoji,需要修改设置为utf8mb4 <?php 'mysql' => [ 'charset' => 'utf8mb4', 'collation ...

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

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

  7. ZipInputStream的用法

    package com.example.io; import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.ev ...

  8. C# 代码转化为Java代码

    http://www.tangiblesoftwaresolutions.com/Free_Editions.html Install Instant C# (converts VB.NET code ...

  9. sql2008清空日志

    USE[master] GO ALTER DATABASE MeSizeSNS SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DATABASE MeSizeSNS ...

  10. LoadRunner执行过程报错“Failed to connect to server "xxx.xxx.xxx.xxx:xx":[10060] connetion time out”

    执行性能测试过程中,LR报错: Action.c(6):Error -27796: Failed to connect to server "xxx.xxx.xxx.xxx:xx" ...