HDU2222 Keywords Search(AC自動機模板題)

Problem 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

Author
Wiskey

題意:
AC自動機裸題,問文本中出現了多少個單詞

思路:
參考博客:https://www.cnblogs.com/hyfhaha/p/10802604.html

  1. AC自動機個人理解就是,對於文本串s,在字段樹中找到以s[i]s[i]結尾的最長後綴串。
  2. 如果字典樹當前指向的地方不能再往下走了,那就找一個相對較小的以s[i]s[i]結尾的後綴串,依次類推。
  3. 這個尋找的過程,就對應了fail指針。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;

const int maxn = 1e6 + 7;

struct Trie {
    int son[30],val,fail;
}t[maxn];

int n,cnt;
char s[maxn];

void init() {
    for(int i = 1;i <= cnt;i++) {
        memset(t[i].son,0,sizeof(t[i].son));
        t[i].val = t[i].fail = 0;
    }
    cnt = 0;
}

void insert(char *s) {
    int u = 1,len = strlen(s);
    for(int i = 0;i < len;i++) {
        int v = s[i] - 'a';
        if(!t[u].son[v]) t[u].son[v] = ++cnt;
        u = t[u].son[v];
    }
    t[u].val++;
}

void getFail() {
    queue<int>q;
    q.push(1);t[1].fail = 0;
    for(int i = 0;i < 26;i++) {
        t[0].son[i] = 1;
    }
    while(!q.empty()) {
        int u = q.front();q.pop();
        for(int i = 0;i < 26;i++) {
            int v = t[u].son[i];
            int Fail = t[u].fail;
            if(!v) {
                t[u].son[i] = t[Fail].son[i];continue; //直接讓不存在的兒子指向失配位置
            }
            t[v].fail = t[Fail].son[i];
            q.push(v);
        }
    }
}

int query(char *s) {
    int u = 1,ans = 0,len = strlen(s);
    for(int i = 0;i < len;i++) {
        int v = s[i] - 'a';
        int k = t[u].son[v];
        while(k > 1 && t[k].val != -1) {
            ans += t[k].val;
            t[k].val = -1;
            k = t[k].fail;
        }
        u = t[u].son[v];
    }
    return ans;
}

int main() {
    int T;scanf("%d",&T);
    while(T--) {
        init();
        cnt = 1;
        scanf("%d",&n);
        for(int i = 1;i <= n;i++) {
            scanf("%s",s);
            insert(s);
        }
        getFail();
        scanf("%s",s);
        printf("%d\n",query(s));
    }
    return 0;
}

根節點爲0時候

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;

const int maxn = 1e6 + 7;

struct Trie {
    int son[30],val,fail;
}t[maxn];

int n,cnt;
char s[maxn];

void init() {
    for(int i = 0;i <= cnt;i++) {
        memset(t[i].son,0,sizeof(t[i].son));
        t[i].val = t[i].fail = 0;
    }
    cnt = 0;
}

void insert(char *s) {
    int u = 0,len = strlen(s);
    for(int i = 0;i < len;i++) {
        int v = s[i] - 'a';
        if(!t[u].son[v]) t[u].son[v] = ++cnt;
        u = t[u].son[v];
    }
    t[u].val++;
}

void getFail() {
    queue<int>q;
    q.push(0);t[0].fail = 0;
    while(!q.empty()) {
        int u = q.front();q.pop();
        for(int i = 0;i < 26;i++) {
            int v = t[u].son[i];
            int Fail = t[u].fail;
            if(!v) {
                t[u].son[i] = t[Fail].son[i];continue; //直接讓不存在的兒子指向失配位置
            }
            if(Fail == 0 && t[Fail].son[i] == v) t[v].fail = 0;
            else t[v].fail = t[Fail].son[i];
            q.push(v);
        }
    }
}

int query(char *s) {
    int u = 0,ans = 0,len = strlen(s);
    for(int i = 0;i < len;i++) {
        int v = s[i] - 'a';
        int k = t[u].son[v];
        while(k > 0 && t[k].val != -1) {
            ans += t[k].val;
            t[k].val = -1;
            k = t[k].fail;
        }
        u = t[u].son[v];
    }
    return ans;
}

int main() {
    int T;scanf("%d",&T);
    while(T--) {
        init();
        scanf("%d",&n);
        for(int i = 1;i <= n;i++) {
            scanf("%s",s);
            insert(s);
        }
        getFail();
        scanf("%s",s);
        printf("%d\n",query(s));
    }
    return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章