【HDU2222】 0.6%达成 模板题 果然无聊的时候A题才是最正确的选择啊因为你就会更加无聊

3 篇文章 0 订阅
2 篇文章 0 订阅
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <queue>
using namespace std;
/**
    Reference:http://www.cnblogs.com/procedure2012/archive/2012/01/29/2331460.html
    Thanks to: lpp学长给的模板和讲义
    Pre-Knowledge:http://blog.csdn.net/c0de4fun/article/details/7879597

    Tought:
    没什么好说的了就是AC自动机啊,Pre-knowledge给的是数组实现的,这里是动态分配实现的。
    主要就是晒模板Query函数而已。
*/
struct node
{
    node *fail;
    int count;
    node* next[26];
    node()
    {
        fail = NULL;
        memset(next,0,sizeof(next));
        count = 0;
    }
};
const int MAX_SIZE = 1000010;
const int MAX_WORD  = 62;
char dest[MAX_SIZE];
char word[MAX_WORD];
queue<node*> q;
void AC_ins(char* word,node* root)
{
    int len = strlen(word);
    node* tmp = root;
    for(int i = 0 ; i < len ; i++)
    {
        int k = word[i]-'a';
        if( !tmp->next[k] )
            tmp->next[k] = new node();
        tmp = tmp->next[k];
    }
    tmp->count++;
}
void AC_build(node* root)
{
    root->fail = NULL;
    q.push(root);
    while(!q.empty())
    {
        node* tmp = q.front();
        node* p = NULL;
        q.pop();
        for(int i = 0 ; i  < 26 ; i++)
        {
            if( tmp->next[i] )
            {
                if( tmp == root )
                {
                    tmp->next[i]->fail = root;
                }
                else
                {
                    p = tmp->fail;
                    while(p)
                    {
                        if( p->next[i] )
                        {
                            tmp->next[i]->fail = p->next[i];
                            break;
                        }
                        p = p->fail;
                    }
                    if(!p)
                        tmp->next[i]->fail = root;
                }
                q.push(tmp->next[i]);
            }
        }
    }
}
int AC_query(node* root,char* str)
{
    int cnt = 0;
    node* p = root;
    int len = strlen(str);
    for(int i = 0 ; i < len ; i++)
    {
        int ind = str[i]-'a';

        while(!p->next[ind] && p != root)
            p = p->fail;

        p = p -> next[ind];

        if( p == NULL )
            p = root;

        node* tmp = p;
        while( tmp != root && tmp->count != -1 )
        {
            cnt = cnt + tmp->count; //instead of cnt++? let's try;
            tmp->count = -1;
            tmp = tmp->fail;
        }
    }
    return cnt;
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("B:\\acm\\SummerVacation\\String-I\\B.in","r",stdin);
    freopen("B:\\acm\\SummerVacation\\String-I\\B.out","w",stdout);
#endif
    int T,N;
    while( scanf("%d",&T) != EOF)
    {
        while(T--)
        {
            node* root = new node();
            scanf("%d\n",&N);
            for(int i = 0 ; i < N ; i++)
            {
                gets(word);
                AC_ins(word,root);
            }
            AC_build(root);
            gets(dest);
            int ans = AC_query(root,dest);
            printf("%d\n",ans);
        }
    }
#ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
#endif
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值