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

    题目链接

    统计一段字符串中有多少个模板串在里面出现过

    #include<bits/stdc++.h>
    using namespace std;
    
    const int N=500007;
    
    struct Trie
    {
        int next[N][26];
        int fail[N];// fail[i]表示i结点// 
        int end[N];    // end[i]表示以i结点为结尾的模式串个数 
        int L,root;    
        int newnode()
        {
            for(int i=0;i<26;i++)
                next[L][i]=-1;
            end[L++]=0;
            return L-1;
        }
        void init()
        {
            L=0;
            root=newnode();
        }
        void insert(char buf[])
        {
            int now=root;
            for(int i=0;buf[i];i++)
            {
                int ch=buf[i]-'a';
                if(next[now][ch]==-1)
                    next[now][ch]=newnode();
                now=next[now][ch];
            }
            end[now]++;
        }
        void build()
        {
            queue<int> Q;
            fail[root]=root;
            for(int i=0;i<26;i++)
            {
                if(next[root][i]==-1)
                    next[root][i]=root;
                else
                {
                    fail[next[root][i]]=root;
                    Q.push(next[root][i]);
                }
            }
            while(!Q.empty())
            {
                int now=Q.front();
                Q.pop();
                for(int i=0;i<26;i++)
                {
                    if(next[now][i]==-1)
                        next[now][i]=next[fail[now]][i];//
                    else
                    {
                        fail[next[now][i]]=next[fail[now]][i];//
                        Q.push(next[now][i]);
                    }
                }
            }
        }
        int query(char buf[])
        {
            int now=root;
            int temp,ret=0;
            for(int i=0;buf[i];i++)
            {
                int ch=buf[i]-'a';
                now=next[now][ch];
                temp=now;
                while(temp!=root)
                {
                    ret+=end[temp];
                    end[temp]=0;    //不作重复计数 
                    temp=fail[temp];
                }
            }
            return ret;
        }
    }ac;
    
    char buf[1000007];
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--)
        {
            char str[57];
            int n;
            ac.init();
            scanf("%d",&n);
            for(int i=0;i<n;i++)
                scanf("%s",str),ac.insert(str);
            ac.build();
            scanf("%s",buf);
            printf("%d
    ",ac.query(buf));
        }
    }
    View Code
  • 相关阅读:
    4. Postman测试集的批量执行(转)
    3. Postman Tests断言(转)
    2. Postman发送各种格式请求的方法
    1. Postman的安装
    2. Django创建项目
    Redis学习笔记(一)
    Oracle连接查询
    Redis学习笔记(五)- 数据类型之set类型
    Redis学习笔记(四)-数据类型之list类型
    PL/SQL实现JAVA中的split()方法的小例子
  • 原文地址:https://www.cnblogs.com/Just--Do--It/p/6479315.html
Copyright © 2011-2022 走看看