思路:

因为不同病毒特征码不会相同。

AC自动机,然后对于每一个输出即可。

注意:以上字符串中字符都是ASCII码可见字符(不包括回车);G++ MLE。

//#include <bits/stdc++.h>
#include<iostream>
#include<queue>
#include<cstdio>
#include<string.h>
#include<algorithm>
using namespace std; const int N=1e5+10;    //500个串,长度为200 struct Trie{
    int num;
    Trie *next[128],*fail;
};
Trie q[N],*root;
int tol; Trie* Creat()
{
    Trie *p;
    p=&q[tol++];
    p->fail=NULL;
    p->num=0;
    for(int i=0;i<128;i++)
        p->next[i]=NULL;
    return p;
} void Insert(char *str,int num)
{
    int len=strlen(str),index;
    Trie *p=root;
    for(int i=0;i<len;i++)
    {
        index=str[i];
        if(p->next[index]==NULL)
            p->next[index]=Creat();
        p=p->next[index];
    }
    p->num=num;
} void Build_Ac()
{
    queue<Trie*>que;
    que.push(root);
    while(!que.empty())
    {
        Trie *p=que.front();que.pop();
        for(int i=0;i<128;i++)
        {
            if(p->next[i]!=NULL)
            {
                if(p==root) p->next[i]->fail=root;
                else{
                    Trie *temp=p->fail;
                    while(temp!=NULL)
                    {
                        if(temp->next[i]!=NULL) {
                            p->next[i]->fail=temp->next[i];
                            break;
                        }
                        temp=temp->fail;
                    }
                    if(temp==NULL)
                        p->next[i]->fail=root;
                }
                que.push(p->next[i]);
            }
        }
    }
} int ans[510],nn;
char word[10010];
void Query()
{
    Trie *p=root;
    int index,len=strlen(word);
    for(int i=0;i<len;i++)
    {
        index=word[i];
        while(p!=root && p->next[index]==NULL)
            p=p->fail;
        p=p->next[index];
        if(p==NULL)
            p=root;
        Trie *temp=p;
        while(temp!=root)
        {
            if(temp->num)
                ans[nn++]=temp->num;
            temp=temp->fail;
        }
    }
} char s[220];
int main()
{
    tol=0;
    root=Creat();
    int n,m;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%s",s);
        Insert(s,i);
    }
    Build_Ac();
    scanf("%d",&m);
    int sum=0;
    for(int i=1;i<=m;i++)
    {
        scanf("%s",word);
        nn=0;
        Query();
        if(nn)
        {
            sum++;
            sort(ans,ans+nn);
            printf("web %d:",i);
            for(int i=0;i<nn;i++)
                printf(" %d",ans[i]);
            puts("");
        }
    }
    printf("total: %d\n",sum);
    return 0;
}

HDU2896【AC自动机-模板】的更多相关文章

  1. HDu-2896 病毒侵袭,AC自动机模板题!

    病毒侵袭 模板题,不多说了.. 题意:n个不同的字符串分别代表病毒特征,给出m次查询,每次一个字符串(网址),求这个字符串中有几个病毒特征,分别从大到小输出编号,最后输出所有的带病毒网址个数.格式请看 ...

  2. HDU 2222 AC自动机模板题

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...

  3. Match:Keywords Search(AC自动机模板)(HDU 2222)

    多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...

  4. HDU 3065 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...

  5. HDU 2896 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...

  6. HDU 2222(AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...

  7. HDU 2222 (AC自动机模板题)

    题意: 给一个文本串和多个模式串,求文本串中一共出现多少次模式串 分析: ac自动机模板,关键是失配函数 #include <map> #include <set> #incl ...

  8. HDU2896+AC自动机

    ac自动机 模板题 /* */ #include<stdio.h> #include<string.h> #include<stdlib.h> #include&l ...

  9. hdu 2222 Keywords Search ac自动机模板

    题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...

  10. KMP与AC自动机模板

    HDU 1711 Number Sequence(KMP模板题) http://acm.hdu.edu.cn/showproblem.php?pid=1711 #include<bits/std ...

随机推荐

  1. 虚拟机和主机ping不通,SQL Server无法远程连接的解决方法

    一.虚拟机网络的配置 这里只列一下自己的配置: 1.编辑---虚拟网络编辑器 进行设置 2.设置对应系统 3.还是Ping不通,最后关闭 虚机内的Windows防火墙,可以Ping通,看来Net模式下 ...

  2. IOS开发之Iphone和Ipad应用程序图标和启动动画

    本文转载至 http://blog.csdn.net/yesjava/article/details/8782060 当我们用xcode开发iphone和ipad应用程序的时候,我们可以用一下表中所显 ...

  3. kubernetes-notes--阅读笔记

    文档地址 https://www.huweihuang.com/kubernetes-notes/

  4. [自动化平台系列] - 初次使用 Macaca-前端自动化测试(2)

    接一下来讲一讲api的使用   http://macacajs.github.io/macaca-wd/api/ var _config = { //本程序的host host: 'http://te ...

  5. selenium2 python范例

    selenium2 python范例 下面脚本的功能是:打开谷歌浏览器-->跳转到某个网址-->输入用户名和密码登录-->读取页面内的数据并求和. # coding=utf-8 #编 ...

  6. [haoi2008]玩具命名

    某人有一套玩具,并想法给玩具命名.首先他选择WING四个字母中的任意一个字母作为玩具的基本名字.然后他会根据自己的喜好,将名字中任意一个字母用“WING”中任意两个字母代替,使得自己的名字能够扩充得很 ...

  7. 打造基于Ubuntu+XBMC的家庭媒体中心

    作为一名高清爱好者,一直想配置一台HTPC放家里实现高清播放外加家庭服务器功能.Nvidia的Ion平台自然是高清平台的硬件首选,而家庭媒体中心的软件端则首先考虑开发的已经很成熟的开源利器 XBMC  ...

  8. hdu 2671 shǎ崽 OrOrOrOrz(排序)

    题意:排序后按题目要求输出 思路:排序 #include<iostream> #include<stdio.h> #include<algorithm> using ...

  9. cowboy跨域请求处理

    这几日在使用cowboy开发https服务器的过程中碰到几个问题,这里随手记录一下. 1)如果返回错误ERR_EMPTY_RESPONSE,那么可能是web服务器被关闭了. 2)如果返回错误ERR_C ...

  10. I.MX6 Surfaceflinger 机制

    /**************************************************************************** * I.MX6 Surfaceflinger ...