zoukankan      html  css  js  c++  java
  • Hdu2222 Keywords Search

    Keywords Search

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


    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
    题目大意:给定n个单词和一篇文章,问有多少个单词出现在文章里了.
    分析:AC自动机模板题.多模式匹配问题.
    #include <cstdio>
    #include <queue>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    const int maxn = 10010 * 55;
    
    int T, n, tot = 1, vis[maxn], ans;
    char s[maxn * 2];
    
    struct node
    {
        int tr[30], cnt, fail;
        void clear()
        {
            memset(tr, 0, sizeof(tr));
            cnt = 0;
            fail = 0;
        }
    }e[maxn];
    
    void insert(char *ss)
    {
        int len = strlen(ss + 1);
        int u = 1;
        for (int i = 1; i <= len; i++)
        {
            int ch = ss[i] - 'a';
            if (!e[u].tr[ch])
            {
                e[u].tr[ch] = ++tot;
                e[tot].clear();
            }
            u = e[u].tr[ch];
        }
        e[u].cnt++;
    }
    
    void build()
    {
        for (int i = 0; i < 26; i++) //易错
            e[0].tr[i] = 1;
        queue <int> q;
        q.push(1);
        while (!q.empty())
        {
            int u = q.front();
            q.pop();
            int fail = e[u].fail;
            for (int i = 0; i < 26; i++)
            {
                int y = e[u].tr[i];
                if (y)
                {
                    e[y].fail = e[fail].tr[i];
                    q.push(y);
                }
                else
                    e[u].tr[i] = e[fail].tr[i];
            }
        }
        /*
        for (int i = 1; i <= tot; i++)
            cout << i << " " << e[i].fail << endl;
            */
    }
    
    void solve()
    {
        scanf("%s", s + 1);
        int len = strlen(s + 1),u = 1;
        for (int i = 1; i <= len; i++)
        {
            int p = s[i] - 'a';
            while (u && !e[u].tr[p])
                u = e[u].fail;
            u = e[u].tr[p];
            int temp = u;
            //cout << u << endl;
            while (temp && e[temp].cnt != -1)
            {
                ans += e[temp].cnt;
                e[temp].cnt = -1;
                temp = e[temp].fail;
            }
        }
    }
    
    int main()
    {
        scanf("%d", &T);
        while (T--)
        {
            ans = 0;
            e[tot = 1].clear();
            scanf("%d", &n);
            for (int i = 1; i <= n; i++)
            {
                scanf("%s", s + 1);
                insert(s);
            }
            build();
            solve();
            printf("%d
    ", ans);
        }
    
        return 0;
    }
  • 相关阅读:
    Android上的远程调试
    使用Chrome DevTools的Timeline和Profiles提高Web应用程序的性能
    用Chrome开发者工具做JavaScript性能分析
    查看html元素绑定的事件与方法的利器
    Chrome的JS调试工具
    作为Web开发人员,我为什么喜欢Google Chrome浏览器
    构造方法
    方法的带参
    OOP类和对象
    循环结构进阶
  • 原文地址:https://www.cnblogs.com/zbtrs/p/8052329.html
Copyright © 2011-2022 走看看