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

    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<queue>
    #include<cstring>
    #include<cstdio>
    using namespace std;
    int ch[10002*52][26],End[10002*52],cur,fail[10002*52],last[10002*52],ans[10002];
    char str[1000005],str0[10002][52];
    int sum[10002*52];
    void get_fail()
    {
        int now,tmpFail,Next;
        queue<int> q;
        for(int j=0; j<26; j++)
        {
            if(ch[0][j])
            {
                q.push(ch[0][j]);
                fail[ch[0][j]] = 0;
                last[ch[0][j]] = 0;
            }
        }
        while(!q.empty())
        {
            now = q.front();
            q.pop();
            for(int j=0; j<26; j++)
            {
                if(!ch[now][j]) continue;
                Next = ch[now][j];
                q.push(Next);
                tmpFail = fail[now];
                while(tmpFail&&!ch[tmpFail][j]) tmpFail = fail[tmpFail];
                fail[Next] = ch[tmpFail][j];
                last[Next] = End[fail[Next]] ? fail[Next]:last[fail[Next]];
            }
        }
    }
    void Find ()
    {
        int now = 0;
        int len = strlen(str);
        for(int i=0; i<len; i++)
        {
            if(str[i]<'a'||str[i]>'z')
            {
                now=0;
                continue;
            }
            str[i]-='a';
            while(now&&!ch[now][str[i]]) now = fail[now];
            now = ch[now][str[i]];
            if(End[now]) ans[End[now]]++;
            int tmp = now;
            while(last[tmp])
            {
                ans[End[last[tmp]]]++;
                tmp = last[tmp];
            }
        }
    }
    int main()
    {
        int n,now,T;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d",&n);
            memset(ch,0,sizeof(ch));
            memset(End,0,sizeof(End));
            memset(ans,0,sizeof(ans));
            memset(last,0,sizeof(last));
            memset(sum,0,sizeof(sum));
            cur = 1;
            int len;
            for(int i=1; i<=n; i++)
            {
                scanf("%s",str0[i]);
                len = strlen(str0[i]);
                now = 0;
                for(int j=0; j<len; j++)
                {
                    str0[i][j]-='a';
                    if(ch[now][str0[i][j]]==0) ch[now][str0[i][j]] = cur++;
                    now = ch[now][str0[i][j]];
                    str0[i][j]+='a';
                }
                if(End[now]) sum[End[now]]++;
                else {
                    sum[i] = 1;
                    End[now] = i;
                }
            }
            get_fail();
            scanf("%s",str);
            Find();
            int res=0;
            for(int i=1; i<=n; i++)
            {
                if(ans[i]) res+=sum[i];
                //sum+=ans[i];
            }
            printf("%d
    ",res);
        }
    }
  • 相关阅读:
    程序基址,X64Dbg软件常用调试技巧查找系统函数调用位置执行到指定位置断点
    #pragma的常用方法讲解,为什么有了条件编译符号“DEBUG”还要来个Debugger.IsAttached
    JDK17Src0.java.base
    nmon的安装和使用
    64位下的相对指令地址X86指令格式(操作码列和指令列解释)
    内存中的程序剖析
    Linux I/O 原理和 Zerocopy 技术全面揭秘
    Ubuntu命令行的垃圾箱,回收站
    SecureCRT密钥链接阿里云
    HTTP API 认证授权术
  • 原文地址:https://www.cnblogs.com/lastone/p/5341598.html
Copyright © 2011-2022 走看看