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

简介: 病毒侵袭持续中  Problem's Link:http://acm.hdu.edu.cn/showproblem.php?pid=3065   Mean:  略 analyse:  AC自动机的运用. 这一题需要将模式串都存储下来,还有就是base的取值一定要弄清楚,由于这题的模式串都是大写字母所以我们可以通过剪枝来加速。

 

病毒侵袭持续中 

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


 

Mean: 

analyse:

 AC自动机的运用.

这一题需要将模式串都存储下来,还有就是base的取值一定要弄清楚,由于这题的模式串都是大写字母所以我们可以通过剪枝来加速。

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

 

Source code:

// Memory   Time
// 1347K     0MS
// by : Snarl_jsb
// 2014-09-30-21.00
#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;

char backup [ 1002 ][ 53 ];
int res [ 1002 ];
const int N = 1010;
char str [ 2000010 ];
struct node
{
    node * next [ 26 ];     //  每个结点都对应26个字母的指针
    node * fail;     //      失配指针
    int count;       //
    int num;
    node()       //  构造函数初始化
    {
        for( int i = 0; i < 26; i ++)
            next [ i ] = NULL;
        count = 0;
        num = 0;
        fail = NULL;
    }
} * 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 ] - 'A'; //  转化为相对数字来存
        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 < 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 [ tail ++ ] = temp -> next [ i ];
            }
        }
    }
}

int Query( node * root)       //  匹配 + 统计
{
    int i = 0 , cnt = 0 , index;
    node *p = root;
    while( str [ i ])
    {
        index = str [ i ] - 'A';
        if( index < 0|| index > 25)   ///这个地方要特别注意,由于病毒只包含大写字母,所以这儿需要剪枝,不剪枝的话其他地方加判断也可以过
        {
           p = root;
            i ++;
            continue;
        }
        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 > 0)   //统计--如果匹配成功,那么count>1,表示该结点代表的单词数量;否则表示该结点没有单词
        {
//            cnt += temp->count; //统计该单词出现的次数
            res [ temp -> num ] ++;   //每次回溯都会加1
//            temp->count = -1;   //!!!!!!!!!!!!!!!!!(如果要重复统计,请讲这句去掉)!!!!!!!!标记为-1,表示该单词已经加入了cnt中
            temp = temp -> fail; //判断整条链上的匹配情况
        }
        i ++;
    }
    return cnt;
}

int main()
{
    int n , m;
    while( cin >>n)
    {
        head = tail = 0;     //  清零
        root = new node();       //  申请新的root结点
        memset( backup , 0 , sizeof( backup));
        memset( res , 0 , sizeof( res));
        for( int i = 1; i <=n; ++ i)
        {
            scanf( "%s" , str);
            strcpy( backup [ i ], str);
            Insert( str , i);
        }
        build_ac_automation( root);
        scanf( "%s" , str);
        Query( root);
        for( int i = 1; i <=n; ++ i)
        {
            if( res [ i ])
            {
                printf( "%s: %d \n " , backup [ i ], res [ i ]);
            }
        }
    }
    return 0;
}

 

目录
相关文章
|
7月前
|
算法 程序员
【Leetcode】NC31 第一个只出现一次的字符(牛客网)、面试题 01.01. 判定字符是否唯一
题目描述: 描述 在一个长为n字符串中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写).(从0开始计数)
38 0
|
5月前
【每日一题Day237】LC1375二进制字符串前缀一致的次数 | 技巧题
【每日一题Day237】LC1375二进制字符串前缀一致的次数 | 技巧题
19 1
|
9月前
|
算法 C语言 C++
Leetcode 每日一题 1234. 替换子串得到平衡字符串
有一个只含有 'Q', 'W', 'E', 'R' 四种字符,且长度为 n 的字符串
30 0
Leetcode 每日一题 1234. 替换子串得到平衡字符串
|
10月前
|
算法
|
11月前
|
算法
每日一题——找出字符串中第一个匹配项的下标(实现strStr)(暴力解法)
每日一题——找出字符串中第一个匹配项的下标(实现strStr)(暴力解法)
|
12月前
|
算法 C++ Python
每日算法系列【LeetCode 424】替换后的最长重复字符
每日算法系列【LeetCode 424】替换后的最长重复字符
每日一题---判断输入的字符串是否为回文
每日一题---判断输入的字符串是否为回文
每日一题---判断输入的字符串是否为回文
|
存储 算法 C语言
串、串的模式匹配算法(子串查找)BF算法、KMP算法
串、串的模式匹配算法(子串查找)BF算法、KMP算法
LeetCode每日一题-9:替换后的最长重复字符串
LeetCode每日一题-9:替换后的最长重复字符串
LeetCode每日一题——1624. 两个相同字符之间的最长子字符串
子字符串 是字符串中的一个连续字符序列。
67 0