zoukankan      html  css  js  c++  java
  • HDU 2222 Keywords Search

    http://acm.hdu.edu.cn/showproblem.php?pid=2222

    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

     代码:

    #include <bits/stdc++.h>
    using namespace std;
    
    struct Trie {
        int nx[500010][26], fail[500010], endd[500010];
        int root, L;
        
        int newnode() {
            for(int i = 0; i < 26; i ++)
                nx[L][i] = -1;
            endd[L ++] = 0;
            return L - 1;
        }
        
        void init() {
            L = 0;
            root = newnode();
        }
        
        void Insert(char buf[]) {
            int len = strlen(buf);
            int now = root;
            for(int i = 0; i < len; i ++) {
                if(nx[now][buf[i] - 'a'] == -1)
                    nx[now][buf[i] - 'a'] = newnode();
                now = nx[now][buf[i] - 'a'];
            }
            endd[now] ++;
        }
        
        void build() {
            queue<int> Q;
            fail[root] = root;
            for(int i = 0; i < 26; i ++) {
                if(nx[root][i] == -1)
                    nx[root][i] = root;
                else {
                    fail[nx[root][i]] = root;
                    Q.push(nx[root][i]);
                }
            }
                
            while(!Q.empty() ) {
                int now = Q.front();
                Q.pop();
                for(int i = 0; i < 26; i ++) {
                    if(nx[now][i] == -1)
                        nx[now][i] = nx[fail[now]][i];
                    else {
                        fail[nx[now][i]] = nx[fail[now]][i];
                        Q.push(nx[now][i]);
                    }
                }
            }
        }
        
        int query(char buf[]) {
            int len = strlen(buf);
            int now = root;
            int res = 0;
            for(int i = 0; i < len; i ++) {
                now = nx[now][buf[i] - 'a'];
                int temp = now;
                while( temp != root ) {
                    res += endd[temp];
                    endd[temp] = 0;
                    temp = fail[temp];
                }
            }
            return res;
        }
        
        void debug() {
            for(int i = 0; i < L; i ++) {
                printf("id = %3d,fail = %3d,end = %3d,chi = [", i, fail[i], endd[i]);
                for(int j = 0; j < 26; j ++)
                    printf("%2d", nx[i][j]);
                printf("]
    ");
            }
        }
    };
    
    char buf[1000010];
    Trie ac;
    
    int main() {
        int T;
        int n;
        scanf("%d", &T);
        while(T --) {
            scanf("%d", &n);
            ac.init();
            for(int i = 0; i < n; i ++) {
                scanf("%s", buf);
                ac.Insert(buf);
            }
            ac.build();
            scanf("%s", buf);
            printf("%d
    ", ac.query(buf));
        }
        return 0;
    }
    

      AC 自动机 kuangbin 的板子

    #include <bits/stdc++.h>
    using namespace std;
    
    const int maxn = 5e5 + 10;
    int N;
    int trie[maxn][26];
    int cntword[maxn];
    int fail[maxn];
    int cnt = 0;
    
    void insertWords(string s) {
        int root = 0;
        for(int i = 0; i < s.length(); i ++) {
            int nx = s[i] - 'a';
            if(!trie[root][nx])
                trie[root][nx] = ++ cnt;
            root = trie[root][nx];
        }
        cntword[root] ++;
    }
    
    void getfail() {
        queue<int> 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 tp = q.front();
            q.pop();
    
            for(int i = 0; i < 26; i ++) {
                if(trie[tp][i]) {
                    fail[trie[tp][i]] = trie[fail[tp]][i];
                    q.push(trie[tp][i]);
                } else trie[tp][i] = trie[fail[tp]][i];
            }
        }
    }
    
    int query(string s) {
        int now = 0, ans = 0;
        for(int i = 0; i < s.length(); 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 T;
        scanf("%d", &T);
        while(T --) {
            //memset(fail, 0, sizeof(fail));
            memset(trie, 0, sizeof(trie));
            memset(cntword, 0, sizeof(cntword));
            cnt = 0;
            scanf("%d", &N);
            for(int i = 0; i < N; i ++) {
                string s;
                cin >> s;
                insertWords(s);
            }
            fail[0] = 0;
            getfail();
            string t;
            cin >> t;
            printf("%d
    ", query(t));
        }
        return 0;
    }
    
    /*
    
    4
    ash
    bcd
    shex
    sha
    ashe
    
    */
    View Code

    (第二份代码用的另一个板子 好不容易弄清楚结果 TLE  不知道为什么 枯了 心情差到爆炸)

     

  • 相关阅读:
    大数据方向招人难啊!!
    .netcore 急速接入第三方登录,不看后悔
    T-SQL——函数——时间操作函数
    T-SQL——关于XML类型
    机器学习方法
    可读性友好的JavaScript:两个专家的故事
    快速了解 JavaScript ES2019 的五个新增特性
    了解 Vue 的 Compsition API
    使用 JavaScript 操作浏览器历史记录 API
    JavaScript ES 模块:现代化前端编程必备技能
  • 原文地址:https://www.cnblogs.com/zlrrrr/p/10744770.html
Copyright © 2011-2022 走看看