zoukankan      html  css  js  c++  java
  • hdu-2222 AC自动机模板题

    Keywords Search

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
    Total Submission(s): 71959    Accepted Submission(s): 24591


    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自动机,自己写的模板一发过,美滋滋
    #include <iostream>
    #include <fstream>
    #include <sstream>
    #include <cstdlib>
    #include <cstdio>
    #include <cmath>
    #include <string>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <set>
    #include <map>
    #include <list>
    #include <iomanip>
    #include <cctype>
    #include <cassert>
    #include <bitset>
    #include <ctime>
    
    using namespace std;
    
    #define pau system("pause")
    #define ll long long
    #define pii pair<int, int>
    #define pb push_back
    #define mp make_pair
    #define clr(a, x) memset(a, x, sizeof(a))
    
    const double pi = acos(-1.0);
    const int INF = 0x3f3f3f3f;
    const int MOD = 1e9 + 7;
    const double EPS = 1e-9;
    
    int T, n, ans;
    char s[10015], t[1000015];
    struct trie {
        char c;
        int vis, is_end, index;
        int fail;
        trie *nex[26];
    } *root;
    trie *create_node(char c) {
        trie *T = new trie;
        T -> c = c;
        T -> vis = T -> is_end = T -> index = 0;
        T -> fail = 0;
        for (int i = 0; i < 26; ++i) {
            T -> nex[i] = NULL;
        }
        return T;
    }
    void Insert(int l) {
        trie *tT = root;
        for (int i = 1; i <= l; ++i) {
            int x = s[i] - 'a';
            if (NULL == tT -> nex[x]) {
                tT -> nex[x] = create_node(s[i]);
            }
            tT = tT -> nex[x];
            if (i == l) {
                ++tT -> is_end;
            }
        }
    }
    trie *arr[261111];
    int index;
    void get_fail() {
        queue<int> que;
        que.push(1);
        arr[1] = root;
        while (que.size()) {
            int x = que.front(); que.pop();
            for (int i = 0; i < 26; ++i) {
                if (NULL == arr[x] -> nex[i]) continue;
                arr[++index] = arr[x] -> nex[i];
                arr[x] -> nex[i] -> index = index;
                int y = arr[x] -> fail;
                while (y && NULL == arr[y] -> nex[i]) {
                    y = arr[y] -> fail;
                }
                if (!y) {
                    arr[index] -> fail = 1;
                } else {
                    arr[index] -> fail = arr[y] -> nex[i] -> index;
                }
                que.push(index);
            }
        }
    }
    void go() {
        trie *tT = root;
        for (int i = 1; t[i]; ++i) {
            int x = t[i] - 'a';
            while (tT != root && NULL == tT -> nex[x]) {
                tT = arr[tT -> fail];
            }
            if (root != tT || tT -> nex[x]) {
                tT = tT -> nex[x];
            }
            tT -> vis = 1;
        }
        for (int i = 1; i <= index; ++i) {
            if (arr[i] -> vis) {
                int y = arr[i] -> fail;
                while (y && !arr[y] -> vis) {
                    arr[y] -> vis = 1;
                    y = arr[y] -> fail;
                }
            }
        }
        for (int i = 1; i <= index; ++i) {
            ans += arr[i] -> vis * arr[i] -> is_end;
        }
    }
    int main() {
        scanf("%d", &T);
        while (T--) {
            root = create_node('#');
            ans = 0, index = 1;
            scanf("%d", &n);
            for (int i = 1; i <= n; ++i) {
                scanf("%s", s + 1);
                int l = strlen(s + 1);
                Insert(l);
            }
            get_fail();
            /*for (int i = 1; i <= index; ++i) {
                printf("arr[%d] c = %c, is_end = %d, fail = %d
    ", i, arr[i] -> c, arr[i] -> is_end, arr[i] -> fail);
            }*/
            scanf("%s", t + 1);
            go();
            printf("%d
    ", ans);
        }
        return 0;
    }
  • 相关阅读:
    WPF 跨应用程序域的 UI(Cross AppDomain UI)
    Visual->UIElement->FrameworkElement,带来更多功能的同时也带来了更多的限制
    使用不安全代码将 Bitmap 位图转为 WPF 的 ImageSource 以获得高性能和持续小的内存占用
    从 “x is null 和 x == null” 的区别看 C# 7 模式匹配中常量和 null 的匹配
    WPF 和 UWP 中,不用设置 From 或 To,Storyboard 即拥有更灵活的动画控制
    WPF 同一窗口内的多线程 UI(VisualTarget)
    如何实现一个可以用 await 异步等待的 Awaiter
    使用 Task.Wait()?立刻死锁(deadlock)
    使用 ExceptionDispatchInfo 捕捉并重新抛出异常
    CaptureMouse/CaptureStylus 可能会失败
  • 原文地址:https://www.cnblogs.com/BIGTOM/p/8445714.html
Copyright © 2011-2022 走看看