AC自动机

http://www.cnblogs.com/Lyush/archive/2011/09/04/2165975.html

题目 http://acm.hdu.edu.cn/showproblem.php?pid=2222

#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <queue>
using namespace std;

struct Node
{
    int end;
    Node *fail, *child[26];
};

char s[1000005];

void getstr( char *s )
{
    char c;
    int p = 0;
    while( c = getchar(), c == ' ' || c == '\n' ) ;
    s[p++] = c;
    while( c = getchar(), c != ' ' && c != '\n' )
        s[p++] = c;
    s[p] = '\0';
}

Node *init(  )
{
    Node *r = ( Node * )malloc( sizeof( Node ) );
    r->end = 0;
    r->fail = NULL;
    memset( r->child, NULL, sizeof( r->child ) );
    return r;
}

void creattree( Node *p, char *in )
{
    int index = *in - 'a';
    if( p->child[index] == NULL )
        p->child[index] = init();
    if( *( in + 1 ) )
        creattree( p->child[index], in + 1 );
    else
        p->child[index]->end++;
}

void creatfail( Node *p )
{
    queue< Node * >q;
    q.push( p );
    while( !q.empty() )
    {
        Node *pos = q.front();
        q.pop();
        for( int i = 0; i < 26; ++i )
        {
            Node *f = pos->fail;
            if( pos->child[i] )
            {
                while( f != NULL )
                {
                    if( f->child[i] )
                    {
                        pos->child[i]->fail = f->child[i];
                        break;
                    }
                    else
                        f = f->fail;
                }
                if( f == NULL )
                {
                    pos->child[i]->fail = p;
                }
                q.push( pos->child[i] );
            }
        }
    }
}

int acauto( Node *p, char *text )
{
    int ans = 0, lenth = 0, len = strlen( text );
    Node *f = p;
    for( int i = 0; i < len; ++i )
    {
        int type = text[i] - 'a';
        while( f )
        {
            if( f->child[type] )
            {
                f = f->child[type];
                break;
            }
            else
                f = f->fail;
        }

        if( f == NULL ) // 如果都已经找到了根节点仍不匹配
        {
            f = p; // 从头开始
            continue;
        }

        Node *t = f;
        while( t != p ) // 成功匹配一个字符的更新操作
        {
            if( t->end )
            {
                ans += t->end;
                t->end = 0;
            }
            t = t->fail;
        }
    }
    return ans;
}

void _free( Node *p )
{
    for( int i = 0; i < 26; ++i )
    {
        if( p->child[i] )
            _free( p->child[i] );
    }
    free( p );
}

int main()
{
    int T;
    scanf( "%d\n", &T );
    while( T-- )
    {
        int N;
        Node *p = init();
        scanf( "%d", &N );
        for( int i = 0; i < N; ++i )
        {
            getstr( s );
            creattree( p, s );
        }
        getstr( s );
        creatfail( p );
        printf( "%d\n", acauto( p, s ) );
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值