HDU 2222 Keywords Search (AC自動機 經典)

版權聲明:可以轉載,但需註明出處,謝謝。 https://blog.csdn.net/Zhao_Xinhao/article/details/77524750

原題

Keywords Search

Time Limit: 1000MS Memory Limit: 131072KB 64bit IO Format: %I64d & %I64u

Description

In the modern time, Search engine came into the life of everybody like Google, Baidu, etc.
Wiskey also wants to bring this feature to his image retrieval system.
Every image have a long description, when users type some keywords to find the image, the system will match the keywords with description of image and show the image which the most keywords be matched.
To simplify the problem, giving you a description of image, and some keywords, you should tell me how many keywords will be match.

Input

First line will contain one integer means how many cases will follow by.
Each case will contain two integers N means the number of keywords and N keywords follow. (N <= 10000)
Each keyword will only contains characters 'a'-'z', and the length will be not longer than 50.
The last line is the description, and the length will be not longer than 1000000.

Output

Print how many keywords are contained in the description.

Sample Input

1
5
she
he
say
shr
her

yasherhs 


Sample Output

3


題意


輸出多模式串在主串中匹配的次數。


涉及知識及算法


AC自動機 傳送門:點擊打開鏈接

代碼

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=1e6+5;
char str2[55];
char str1[maxn];

struct node
{
    node *Next[26];
    //失配指針 指向與p相同的結點,若沒有則指向root
    node *fail;
    //以該點爲單詞結尾個數
    int sum;
    node()
    {
        sum=0;
        fail=NULL;
        for(int i=0;i<26;i++)
        {
            Next[i]=NULL;
        }
    }
};
node* root;
node *q[maxn];
//字典樹的建立
void Insert(char *s)
{
    node *p=root;
    for(int i=0;s[i];i++)
    {
        int x=s[i]-'a';
        if(p->Next[x]==NULL)
        {
            p->Next[x]=new node;
        }
        p=p->Next[x];
    }
    p->sum++;
}
//用BFS實現構造fail指針
void bulid_fail_pointer()
{
    int head=0;
    int tail=1;
    q[head]=root;
    //p仍指向當前匹配的字符
    //temp爲測試指針 temp的子節點的fail指向的結點即爲p結點
    //在建立fail指針時有尋找與p字符匹配的結點的作用
    node *p,*temp;
    while(head<tail)
    {
        temp=q[head++];
        for(int i=0;i<26;i++)
        {
            if(temp->Next[i])
            {
                if(temp==root)
                {
                    //第一層的結點的fail只能指向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];
            }
        }
    }
}

int ac_automation(char *ch)
{
    int cnt=0;
    node *p=root;
    int len=strlen(ch);
    for(int i=0;i<len;i++)
    {
        int x=ch[i]-'a';
        //如果當前不匹配就去fail那裏指的地方匹配
        while(!p->Next[x]&&p!=root)
        {
            p=p->fail;
        }
        p=p->Next[x];
        //如果p爲NULL說明不匹配,回到根
        if(!p)
        {
            p=root;
        }
        node *temp=p;
        //如果匹配計算個數
        while(temp!=root)
        {
            if(temp->sum>=0)
            {
                cnt+=temp->sum;
                temp->sum=-1;
            }
            else
            {
                break;
            }
            //跳轉到fail指針指向的結點
            temp=temp->fail;
        }
    }
    return cnt;
}

void Clear(node* a)              //釋放內存
{
    if(a==NULL) return;
    else
    {
        for(int i=0;i<26;i++)
        {
            Clear(a->Next[i]);
        }
    }
    delete (a);
}

int main()
{
    //freopen("in.txt","r",stdin);
    int T,n;
    scanf("%d",&T);
    while(T--)
    {
        root=new node;
        scanf("%d",&n);
        while(n--)
        {
            scanf("%s",str2);
            Insert(str2);
        }
        scanf("%s",str1);
        bulid_fail_pointer();
        printf("%d\n",ac_automation(str1));
        Clear(root);
    }
    return 0;
}

代碼參考了CSDN博主creatorx的文章,鏈接在上方,表示感謝。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章