ac自动机(KMP+字典树)

//数组做法
#include
using namespace std;

int trie[100000][26]; //字典树
int cntword[100000];  //记录该单词出现次数
int fail[100000];     //失败时的回溯指针
int cnt = 0;

void Insert(char s[])
{
    int root = 0;
    for(int i=0;s[i]!= '\0'; i++)
    {
        int next = s[i] - 'a';
        if(!trie[root][next])
        {
            trie[root][next] = ++cnt;
        }
        root = trie[root][next];
    }
    cntword[root]++;      //当前节点单词数+1
}
void getFail()
{
    queue q;
    for(int i=0; i<26; i++)
    {
        if(trie[0][i])
        {
            fail[trie[0][i]] = 0;
            q.push(trie[0][i]);
        }
    }

    while(!q.empty())
    {
        int now = q.front();
        q.pop();

        for(int i=0; i<26; i++)
        {
            if(trie[now][i])
            {
                fail[trie[now][i]] = trie[fail[now]][i];
                q.push(trie[now][i]);
            }
            else
                trie[now][i] = trie[fail[now]][i];
        }
    }
}


int ac_machine(char s[])
{
    int now = 0,ans = 0;
    for(int i=0;s[i]!='\0'; i++)
    {
        now = trie[now][s[i]-'a'];
        for(int j=now; j && cntword[j]!=-1; j=fail[j])
        {
            ans += cntword[j];
            cntword[j] = -1;
        }
    }
    return ans;
}

int main()
{
    int n;
    char s[12000];
    scanf("%d",&n);
    for(int i=0; i
//指针做法
#include
using namespace std;
const int maxn = 1e7 + 5;
const int MAX = 10000000;
int cnt;
struct node
{
    node *next[26];
    node *fail;
    int sum;
};
node *root,*newnode,*q[MAX];
char key[70];
int head,tail;
char pattern[maxn];
int N;

struct node* init()
{
    struct node * p=(struct node *)malloc(sizeof(struct node));
    for(int j=0; j<26; j++)
    {

        p->next[j] = 0;
    }
    p->fail = 0;
    p->sum = 0;
    return p;
}
void Insert(char *s)
{
    node *p = root;
    for(int i = 0; s[i]; i++)
    {
        int x = s[i] - 'a';
        if(p->next[x] == NULL)
        {
            newnode= init();
            p->next[x]=newnode;
        }
        p = p->next[x];
    }
    p->sum++;
}

void build_fail_pointer()
{
    head = 0;
    tail = 1;
    q[head] = root;
    node *p;
    node *temp;
    while(head < tail)
    {
        temp = q[head++];
        for(int i = 0; i <= 25; i++)
        {
            if(temp->next[i])
            {
                if(temp == root)
                {
                    temp->next[i]->fail = root;
                }
                else
                {
                    p = temp->fail;
                    while(p)
                    {
                        if(p->next[i])
                        {
                            temp->next[i]->fail = p->next[i];
                            break;
                        }
                        p = p->fail;
                    }
                    if(p == NULL) temp->next[i]->fail = root;
                }
                q[tail++] = temp->next[i];
            }
        }
    }
}
void ac_automation(char *ch)
{
    node *p = root;
    int len = strlen(ch);
    for(int i = 0; i < len; i++)
    {
        int x = ch[i] - 'a';
        while(!p->next[x] && p != root)
        {
            p = p->fail;

        }
        p = p->next[x];
        if(!p)
            p = root;
        node *temp = p;
        while(temp != root)
        {
            if(temp->sum >= 0)
            {
                cnt += temp->sum;
                temp->sum = -1;
            }
            else
                break;
            temp = temp->fail;
        }
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        root = init();
        scanf("%d",&N);
        for(int i = 1; i <= N; i++)
        {
            scanf("%s",key);
            Insert(key);
        }
        scanf("%s",pattern);
        cnt = 0;
        build_fail_pointer();
        ac_automation(pattern);
        printf("%d\n",cnt);
    }
    return 0;
}


 

你可能感兴趣的:(ac自动机(KMP+字典树))