KeyWords Search(AC automation)

本文介绍了一种使用AC自动机处理字符串匹配问题的方法。通过构建Trie树和失败指针优化了搜索过程,实现了高效查找一组模式串在目标串中出现的所有位置。代码示例展示了从插入模式串到构建失败指针再到查询目标串的具体实现。
#include <cstdio>
#include <cstring>
using namespace std;

const int num_char=26;
struct  TrieNode
{
    TrieNode *branch[num_char];
    TrieNode *fail;
    int count;//if this is the last node of the word
    TrieNode()
    {
        fail=NULL;
        count=0;
        memset(branch,NULL,sizeof(branch));
    }
} *q[500001];
int qhead,qtail;
char src[1000005];
char input[55];
void insert(const char *src,TrieNode *root)
{
    TrieNode *hp=root;
    while(*src!=0)
    {
        if(hp->branch[*src-'a']==NULL)hp->branch[*src-'a']=new TrieNode();
        hp=hp->branch[*src-'a'];
        src++;
    }
    hp->count++; //+1 stands for it's the end of a word.
}

void build_fail(TrieNode *root)
{
    int i;
    root->fail=NULL;
    q[qhead++]=root;
    while(qhead!=qtail)
    {
        TrieNode *temp=q[qtail++];
        TrieNode *p=NULL;
        for(i=0; i<26; i++)
        {
            if(temp->branch[i]!=NULL)
            {
                if(temp==root)temp->branch[i]->fail=root;
                else
                {
                    p=temp->fail;
                    while(p!=NULL)
                    {
                        if(p->branch[i]!=NULL)
                        {
                            temp->branch[i]->fail=p->branch[i];
                            break;
                        }
                        p=p->fail;
                    }
                    if(p==NULL)temp->branch[i]->fail=root;
                }
                q[qhead++]=temp->branch[i];
            }
        }
    }
}
int query(TrieNode *root)
{
    int i=0,cnt=0,index;
    TrieNode *p=root;
    while(src[i]!=0)
    {
        index=src[i]-'a';
        while(p->branch[index]==NULL&& p!=root)p=p->fail;
        p=p->branch[index];
        p=(p==NULL)?root:p;
        TrieNode *temp=p;
        while(temp!=root&&temp->count!=-1)
        {
            cnt+=temp->count;
            temp->count=-1;
            temp=temp->fail;
        }
        i++;
    }
    return cnt;
}

int main()
{
    int n;
    int t;
    scanf("%d",&t);
    while(t--)
    {
        TrieNode *root=new TrieNode();
        qtail=qhead=0;
        scanf("%d",&n);
            for(int i=0; i<n; ++i)
            {
                scanf("%s",input);
                insert(input,root);
            }
            build_fail(root);
          //  printf("input target:\n");
            scanf("%s",src);
            printf("%d\n",query(root));
    }
    return 0;
}





转载于:https://www.cnblogs.com/MicZ/archive/2012/10/24/2785360.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值