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. 高级屏幕空间反射: Screen Space Reflection (SSR)

    自从CE3首倡SSR以来,发展至今,其质量与当年早已不能同日而语.不仅强调超越性的质量,而且强调超越性的性能.乘着周末有空撸了撸,以下是增强型实时SSR结果图.与我原来的SSR原始实现相比,新的增强型 ...

  2. ios开发中的C语言学习—— 结构体简介

    在开发过程中,经常会需要处理一组不同类型的数据,比如学生的个人信息,由姓名.年龄.性别.身高等组成,因为这些数据是由不同数据类型组成的,因此不能用数组表示,对于不同数据类型的一组数据,可以采用结构体来 ...

  3. 高吞吐量的分布式发布订阅消息系统Kafka--安装及测试

    一.Kafka概述 Kafka是一种高吞吐量的分布式发布订阅消息系统,它可以处理消费者规模的网站中的所有动作流数据. 这种动作(网页浏览,搜索和其他用户的行动)是在现代网络上的许多社会功能的一个关键因 ...

  4. 同时大量连接导致的DDOS攻击,导致收发器宕机,用户大面积超时掉线

    前段时间一个客户改成电信网通自动路由后(当然和这个没有关系,但是客户一般没有分析能力,会多想),用户经常大面积掉线,用户才180多个,在线最多也才120多,十分苦恼,原先帮其维护的技术人员,只是远程诊 ...

  5. 在c#程式中配置log4net

    參考網址: http://www.cnblogs.com/kissazi2/p/3393595.html http://www.cnblogs.com/kissazi2/p/3389551.html ...

  6. Sql中的Merge和output

    先看merge, 不用merge时: --更新 update TA Value ) --插入没有的数据 insert into TA ,,Value from TB ) and TypeName=@v ...

  7. 项目管理知识体系指南(PMBOOK指南)(第5版) 阅读摘要

    1.7.2 项目经理的人际技能 领导力: 团队建设: 激励: 沟通: 影响力: 决策能力: 政治和文化意识: 谈判: 建立信任: 冲突管理: 教练技术: 3.4 规划过程组 在制定项目管理计划和项目文 ...

  8. 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" ...

  9. solr searcher

    solr searcher 前面我配置好了solr,并且数据库建立索引也完成了. 为php添加搜索 首先下载solrphp http://wiki.apache.org/solr/SolPHP 在so ...

  10. AX ERP 真正的自动批处理

    AX real batch job- AX ERP 真正的批处理 在AX3标准功能中,自动化任务是利用Batch来进行自动化处理任务,标准功能的局限是无法真正做到无人值守.比如服务器重启,必须手动去开 ...