• hdu 2222 Keywords Search


    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
     
    Recommend
    lcy   |   We have carefully selected several similar problems for you:  2896 3065 2243 2825 3341 
     
    每个样例给n个短字符串,和一个长字符串,问这个长字符串中有几个短字符串出现过,ac自动机模板题。
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <queue>
    #define MAX 1000000
    using namespace std;
    struct Trie {
        Trie *Next[26],*Fail;
        int sum;
        Trie() {
            for(int i = 0;i < 26;i ++) {
                Next[i] = NULL;
            }
            Fail = NULL;
            sum = 0;
        }
    }*root;
    void Insert_Str(char *s) {///字符串插入到字典树中
       Trie *r = root;
       int i = -1;
       while(s[++ i]) {
           int d = s[i] - 'a';
           if(r -> Next[d] == NULL) {
               r -> Next[d] = new Trie();
           }
           r = r -> Next[d];
       }
       r -> sum ++;///结尾加1
    }
    void Build_Fail() {///通过父结点的Fail更新子结点的Fail
        Trie *node,*temp;
        queue<Trie *> q;
        q.push(root);
        while(!q.empty()) {
            node = q.front();
            q.pop();
            for(int i = 0;i < 26;i ++) {
                if(node -> Next[i]) {///第i个儿子存在
                    temp = node -> Fail;///temp赋值当前节点的Fail
                    while(temp) {
                        if(temp -> Next[i]) {
                            node -> Next[i] -> Fail = temp -> Next[i];
                            break;
                        }
                        temp = temp -> Fail;
                    }
                    if(temp == NULL) {///没找到或者本来就是根节点
                        node -> Next[i] -> Fail = root;
                    }
                    q.push(node -> Next[i]);
                }
            }
        }
    }
    int Ac_automation(char *s) {
        int i = -1,ans = 0;
        Trie *node = root,*temp;
        while(s[++ i]) {
            int d = s[i] - 'a';
            while(node != root && node -> Next[d] == NULL) node = node -> Fail;///如果没有匹配的子结点 就找它的Fail看看有没有匹配的子结点
            if(node -> Next[d]) node = node -> Next[d];
            temp = node;
            while(temp && temp -> sum >= 0) {
                ans += temp -> sum;
                temp -> sum = -1;///出现了  再出现时不再计算
                temp = temp -> Fail;///找最长后缀
            }
        }
        return ans;
    }
    int main() {
        int t,n;
        char tr[50],s[MAX];
        scanf("%d",&t);
        while(t --) {
            root = new Trie();
            scanf("%d",&n);
            for(int i = 0;i < n;i ++) {
                scanf("%s",tr);
                Insert_Str(tr);
            }
            Build_Fail();
            scanf("%s",s);
            printf("%d
    ",Ac_automation(s));
        }
    }

     数组实现

    代码:

    #include <iostream>
    #include <cstdio>
    #include <sstream>
    #include <cstring>
    using namespace std;
    int trie[500001][26];
    int fail[500001];
    int pos;
    int sum[500001];
    int q[500001];
    void Insert(char *str) {
        int i = -1,c = 0;
        while(str[++ i]) {
            int d = str[i] - 'a';
            c = trie[c][d] ? trie[c][d] : (trie[c][d] = ++ pos);
        }
        sum[c] ++;
    }
    void build_fail() {
        int head = 0,tail = 0;
        q[tail ++] = 0;
        while(head < tail) {
            int c = q[head ++];
            for(int i = 0;i < 26;i ++) {
                if(trie[c][i]) {
                    int temp = fail[c];
                    while(temp != -1 && !trie[temp][i]) temp = fail[temp];
                    if(temp >= 0) fail[trie[c][i]] = trie[temp][i];
                    q[tail ++] = trie[c][i];
                }
            }
        }
    }
    int ac_automation(char *s) {
        int i = -1,j = 0,c = 0;
        while(s[++ i]) {
            int d = s[i] - 'a';
            while(j > 0 && !trie[j][d]) j = fail[j];
            if(trie[j][d]) j = trie[j][d];
            int temp = j;
            while(temp && sum[temp] >= 0) {
                c += sum[temp];
                sum[temp] = -1;
                temp = fail[temp];
            }
        }
        return c;
    }
    int main() {
        int t,n;
        char s[1000001],str[51];
        scanf("%d",&t);
        while(t --) {
            scanf("%d",&n);
            pos = 0;
            memset(trie,0,sizeof(trie));
            memset(sum,0,sizeof(sum));
            memset(fail,0,sizeof(fail));
            fail[0] = -1;///很重要
            for(int i = 0;i < n;i ++) {
                scanf("%s",str);
                Insert(str);
            }
            build_fail();
            scanf("%s",s);
            printf("%d
    ",ac_automation(s));
        }
    }

     基于字典图的实现

    代码:

    #include <iostream>
    #include <cstdio>
    #include <sstream>
    #include <cstring>
    using namespace std;
    int trie[500001][26];
    int fail[500001];
    int pos;
    int sum[500001];
    int q[500001];
    void Insert(char *str) {
        int i = -1,c = 0;
        while(str[++ i]) {
            int d = str[i] - 'a';
            c = trie[c][d] ? trie[c][d] : (trie[c][d] = ++ pos);
        }
        sum[c] ++;
    }
    void build_fail() {
        int head = 0,tail = 0;
        q[tail ++] = 0;
        while(head < tail) {
            int c = q[head ++];
            for(int i = 0;i < 26;i ++) {
                if(trie[c][i]) {
                    if(c) fail[trie[c][i]] = trie[fail[c]][i];///c不是根结点才可以,否自自己指向自己是不对的
                    q[tail ++] = trie[c][i];
                }
                else trie[c][i] = trie[fail[c]][i];///如果第i儿子不存在,直接指向失配指针的第i个儿子,一层一层往上指
            }
        }
    }
    int ac_automation(char *s) {
        int i = -1,j = 0,c = 0,t;
        while(s[++ i]) {
            int d = s[i] - 'a';
            j = trie[j][d];
            t = j;
            while(t && ~sum[t]) {
                c += sum[t];
                sum[t] = -1;
                t = fail[t];
            }
        }
        return c;
    }
    int main() {
        int t,n;
        char s[1000001],str[51];
        scanf("%d",&t);
        while(t --) {
            scanf("%d",&n);
            pos = 0;
            memset(trie,0,sizeof(trie));
            memset(sum,0,sizeof(sum));
            memset(fail,0,sizeof(fail));
            for(int i = 0;i < n;i ++) {
                scanf("%s",str);
                Insert(str);
            }
            build_fail();
            scanf("%s",s);
            printf("%d
    ",ac_automation(s));
        }
    }
  • 相关阅读:
    FRR and VFR
    MEC in 5G networks
    KNI
    SGX KMRA
    5G E2E issues
    物理层无线信道的特征
    PTP 时钟
    Ransim go through
    网页title闪动JavaScript实现消息提醒
    敏感词库
  • 原文地址:https://www.cnblogs.com/8023spz/p/9786936.html
Copyright © 2020-2023  润新知