• HDU2222(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

    题目的大体意思就是:有T组数据,给出n个单词作为字典,一个s字符串,查找s中出现多少个字典中的单词

    分析:AC自动机必做题,这个自动机是自己搞的,O(∩_∩)O~呵呵
    主要步骤分三步:
    1.构建trie树
    2.构建失配指针
    3.像自动机一样奔跑

    这里写代码片
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<queue>
    
    using namespace std;
    
    int ch[50*10010][30];
    char w[10010][51];
    char s[1000010];
    int n,T,tot,fail[50*10010];
    int iend[50*10010];
    bool p[50*10010];
    
    void build(int bh)
    {
        int i,j,now=0;
        int len=strlen(w[bh]);
        for (i=0;i<len;i++)
        {
            int x=w[bh][i]-'a';
            if (!ch[now][x]) ch[now][x]=++tot;
            now=ch[now][x];
        }
        iend[now]++;
        return;
    }
    
    void make()
    {
        int i,j;
        queue<int> q;
        for (i=0;i<26;i++)
           if (ch[0][i])
              q.push(ch[0][i]);
        while (!q.empty())
        {
            int r=q.front();
            q.pop();
            for (i=0;i<26;i++)
            {
                if (!ch[r][i])
                {
                    ch[r][i]=ch[fail[r]][i];
                    continue;
                }
                fail[ch[r][i]]=ch[fail[r]][i];
                q.push(ch[r][i]);
            }
        }
        return;
    }
    
    void solve()  //0是根节点 
    {
        memset(p,1,sizeof(p));
        int len=strlen(s);
        int ans=0;
        int now=0,i;
        for (i=0;i<len;i++)
        {
            int x=s[i]-'a';
            while (!ch[now][x]&&now!=0) now=fail[now];  //他没有这个儿子,就找失配
            //找到根节点还没找到就停止了 
            now=ch[now][x];  //儿子 
            int f=now;
            while (f)  //回溯,直到根 
            {
                if (!p[f]) break;
                else if (p[f]&&iend[f]>0)  //从未访问过 
                {
                    ans+=iend[f];
                    p[f]=0;
                }
                f=fail[f];  //往失配上找,看看失配上有没有可以匹配的单词 
            }
        }   
        printf("%d
    ",ans);
        return;
    }
    
    int main()
    {
        scanf("%d",&T);
        while (T--)
        {
            memset(iend,0,sizeof(iend));  //千万不要忘记初始化
            memset(ch,0,sizeof(ch));
            memset(fail,0,sizeof(fail));
            tot=0;
            memset(w,'',sizeof(w));
            memset(s,'',sizeof(s));
            scanf("%d",&n);
            for (int i=1;i<=n;i++)
            {
                scanf("%s",&w[i]);
                build(i);
            }
            make();
            scanf("%s",&s);
            solve();
        }
        return 0;
    }
  • 相关阅读:
    【Java】Java 序列化的高级认识
    【随笔】感同身受
    【教训】徐小平:不要用兄弟情谊来追求共同利益,要用共同利益追求兄弟情谊
    【面试】惠普IT电面
    【面试】中兴
    【面试】国金证券
    【298】◀▶ IDL 系统过程&函数
    【297】IDL 过程、函数&关键字参数
    【296】Python 默认 IDE 修改
    【295】暗黑表格模板及相关
  • 原文地址:https://www.cnblogs.com/wutongtong3117/p/7673649.html
Copyright © 2020-2023  润新知