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

2024-09-05 17:32

本文主要是介绍AC自动机 - 多模式串的匹配运用 --- HDU 2896,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

病毒侵袭

Problem's Link:http://acm.hdu.edu.cn/showproblem.php?pid=2896


 

Mean: 

 略

analyse:

AC自动机的运用,多模式串匹配。就是有几个细节要注意,在这些细节上卡了半天了。

1)输出的网站编号和最终的病毒网站数不是一样的;

2)next指针要设128,不然会爆栈;

3)同理,char转换为int时,base要设为31;

Time complexity:o(n)+o(ml) 

 

Source code:

// Memory   Time
// 1347K     0MS
// by : Snarl_jsb
// 2014-09-30-11.01
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<string>
#include<climits>
#include<cmath>
#define LL long long
using namespace std;


const int N = 10010;
char str [N ];
struct node
{
    node * next [ 128 ];     //  每个结点都对应128个字母的指针
    node * fail;     //      失配指针
    int count;       //
    int num;
    node()       //  构造函数初始化
    {
        for( int i = 0; i < 128; i ++)
            next [ i ] = NULL;
        count = 0;
        fail = NULL;
        num = 0;
    }
} * q [ 50 *N ];
node * root;
int head , tail;

void Insert( char * str , int num) //   插入单词.相当于构建一个Trie树
{
    node *p = root;
    int i = 0 , index;
    while( str [ i ]) {
        index = str [ i ] - 31; //  转化为相对数字来存
        if(p -> next [ index ] == NULL) // 该字母未插入过
           p -> next [ index ] = new node();     //  为该字母申请一个结点
       p = p -> next [ index ];     //   移至下一个
        i ++;
    }
   p -> count ++;     //      记录该结点的单词总共插入的次数
   p -> num = num;
}
void build_ac_automation( node * root)         //      bfs建立fail指针
{
    root -> fail = NULL;
    q [ tail ++ ] = root;
    while( head < tail) {
        node * temp = q [ head ++ ];
        node *p = NULL;
        for( int i = 0; i < 128; 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 [ tail ++ ] = temp -> next [ i ];
            }
        }
    }
}
int total = 0;
int Query( node * root , int num)       //  匹配 + 统计
{
    int i = 0 , cnt = 0 , index;
    node *p = root;
    int idx = 0;
    int ans [ 100 ];

    while( str [ i ])
    {
        index = str [ i ] - 31;
        while(p -> next [ index ] == NULL && p != root) //前缀是相同的,所以不管哪个指针走到了count不为0的结点上,那么该结点所代表的单词就匹配成功
           p = p -> fail; //失配情况下,p指针指向p->fail.(相当于KMP的next数组)
       p = p -> next [ index ]; //由于现在所在的位置是父节点,所以需要向下移动一个位置
        if(p == NULL)
           p = root; //如果匹配失败,移动到root,重新开始匹配
        node * temp = p; //
        while( temp != root && temp -> count != - 1)   //统计--如果匹配成功,那么count>1,表示该结点代表的单词数量;否则表示该结点没有单词
        {
            if( temp -> count > 0)
            {
                cnt ++; //统计该单词出现的次数
                ans [ ++ idx ] = temp -> num;
            }
//            temp->count = -1;   //标记为-1,表示该单词已经加入了cnt中,下次就不用重复统计
            temp = temp -> fail; //判断整条链上的匹配情况
        }
        i ++;
    }
    if( idx == 0)
        return 0;
    printf( "web %d: " , num);
    sort( ans + 1 , ans + 1 + idx);
    total ++;
    for( int i = 1; i < idx; ++ i)
    {
        printf( "%d " , ans [ i ]);
    }
    printf( "%d \n " , ans [ idx ]);
    return cnt;
}

int main()
{
    int n;
    cin >>n;
    head = tail = 0;
    root = new node();
    for( int i = 1; i <=n; ++ i)
    {
        scanf( "%s" , str);
        Insert( str , i);
    }
    build_ac_automation( root);       //  建树
    int m;
    cin >> m;
    total = 0;
    for( int i = 1; i <= m; ++ i)
    {
        scanf( "%s" , str);
        Query( root , i);
    }
    printf( "total: %d \n " , total);
    return 0;
}

这篇关于AC自动机 - 多模式串的匹配运用 --- HDU 2896的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1139610

相关文章

Python itertools中accumulate函数用法及使用运用详细讲解

《Pythonitertools中accumulate函数用法及使用运用详细讲解》:本文主要介绍Python的itertools库中的accumulate函数,该函数可以计算累积和或通过指定函数... 目录1.1前言:1.2定义:1.3衍生用法:1.3Leetcode的实际运用:总结 1.1前言:本文将详

Java实现状态模式的示例代码

《Java实现状态模式的示例代码》状态模式是一种行为型设计模式,允许对象根据其内部状态改变行为,本文主要介绍了Java实现状态模式的示例代码,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来... 目录一、简介1、定义2、状态模式的结构二、Java实现案例1、电灯开关状态案例2、番茄工作法状态案例

关于Gateway路由匹配规则解读

《关于Gateway路由匹配规则解读》本文详细介绍了SpringCloudGateway的路由匹配规则,包括基本概念、常用属性、实际应用以及注意事项,路由匹配规则决定了请求如何被转发到目标服务,是Ga... 目录Gateway路由匹配规则一、基本概念二、常用属性三、实际应用四、注意事项总结Gateway路由

【Prometheus】PromQL向量匹配实现不同标签的向量数据进行运算

✨✨ 欢迎大家来到景天科技苑✨✨ 🎈🎈 养成好习惯,先赞后看哦~🎈🎈 🏆 作者简介:景天科技苑 🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。 🏆《博客》:Python全栈,前后端开发,小程序开发,人工智能,js逆向,App逆向,网络系统安全,数据分析,Django,fastapi

在JS中的设计模式的单例模式、策略模式、代理模式、原型模式浅讲

1. 单例模式(Singleton Pattern) 确保一个类只有一个实例,并提供一个全局访问点。 示例代码: class Singleton {constructor() {if (Singleton.instance) {return Singleton.instance;}Singleton.instance = this;this.data = [];}addData(value)

usaco 1.3 Mixing Milk (结构体排序 qsort) and hdu 2020(sort)

到了这题学会了结构体排序 于是回去修改了 1.2 milking cows 的算法~ 结构体排序核心: 1.结构体定义 struct Milk{int price;int milks;}milk[5000]; 2.自定义的比较函数,若返回值为正,qsort 函数判定a>b ;为负,a<b;为0,a==b; int milkcmp(const void *va,c

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2093 考试排名(sscanf)

模拟题。 直接从教程里拉解析。 因为表格里的数据格式不统一。有时候有"()",有时候又没有。而它也不会给我们提示。 这种情况下,就只能它它们统一看作字符串来处理了。现在就请出我们的主角sscanf()! sscanf 语法: #include int sscanf( const char *buffer, const char *format, ... ); 函数sscanf()和

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

hdu 1754 I Hate It(线段树,单点更新,区间最值)

题意是求一个线段中的最大数。 线段树的模板题,试用了一下交大的模板。效率有点略低。 代码: #include <stdio.h>#include <string.h>#define TREE_SIZE (1 << (20))//const int TREE_SIZE = 200000 + 10;int max(int a, int b){return a > b ? a :