【AC自动机】HDU 2222

BabbleDay posted @ 2013年8月12日 17:24 in 刷题防身 , 1179 阅读

Keywords Search

一道AC自动机的模板题,但是第一次敲真是苦逼。。

#include <iostream>
#include <cstdio>
#include <queue>

using namespace std;

const int alphabet = 26;
struct node
{
    node* branch[alphabet];
    node* fail;
    int count;
    node()
    {
        for(int i=0; i<alphabet; i++)
            branch[i] = NULL;
        fail = NULL;
        count = 0;
    }
};

void dictionary(node* root, char word[])
{
    node* p = root;
    int k, i=0;
    while(word[i])
    {
        k = word[i] - 'a';
        if(p->branch[k] == NULL)
            p->branch[k] = new node();
        p = p->branch[k];
        i++;
    }
    p->count++;
}

void build_fail(node* root)
{
    node* p;
    node* f;
    queue<node*> Q;
    root->fail = NULL;
    Q.push(root);
    while(!Q.empty())
    {
        p = Q.front(); Q.pop();
        for(int i=0; i<alphabet; i++)
        {
            if(p->branch[i] != NULL)
            {
                f = p->fail;
                while(f!=NULL)
                {
                    if(f->branch[i] != NULL)
                    {
                        p->branch[i]->fail = f->branch[i]; 
                    break;
                    }
                    f = f->fail;
                }
                if(f==NULL) p->branch[i]->fail = root;
                Q.push(p->branch[i]);
            }
        }
    }
}

int search(node* root, char str[])
{
    int k, i=0, sum=0;
    node* p = root;
    node* temp;
    while(str[i])
    {
        k = str[i] - 'a';
        while(p->branch[k] == NULL && p!=root)
            p = p->fail;
        if(p->branch[k]!=NULL) p = p->branch[k];
        else p = root;
        temp = p;
        while(temp!=root && temp->count!=-1)
        {
            sum += temp->count;
            temp->count = -1;
            temp = temp->fail;
        }
        i++;
    }
    return sum;
}

int main()
{
    int c, n;
    char word[55], str[1000005];
    cin >> c;
    while(c--)
    {
        node* root = new node();
        cin >> n;
        while(n--)
        {
            scanf("%s", &word);
            dictionary(root, word);
        }
        build_fail(root);
        scanf("%s", &str);
        cout << search(root, str) << endl;
    }
    return 0;
}
Avatar_small
Uttarakhand Board Qu 说:
2022年9月15日 21:15

Uttarakhand Board of School Education and various subject experts of the state have designed and suggested the Term1 & Term-2 Exam Solved Question Bank with Answers as UK Board 4th Class Model Paper 2023 with Mock Test and Practice Questions for All Languages and Subjects of the Course for SCERT & NCERT Syllabus. Uttarakhand Board Question Paper Class 4 Every Elementary Level Primary School 4th Standard student Studding in Government & Private Schools of the State & Central Board can download and Practice the Learning & Study Material along with UK Board STD-4 Question Paper 2023 Pdf to now the new exam scheme and that’s helps you to get top scores in all exams.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter