zoukankan      html  css  js  c++  java
  • I

    当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻。。。。在这样的时刻,人们却异常兴奋——我们能在有生之年看到500年一遇的世界奇观,那是多么幸福的事儿啊~~ 
    但网路上总有那么些网站,开始借着民众的好奇心,打着介绍日食的旗号,大肆传播病毒。小t不幸成为受害者之一。小t如此生气,他决定要把世界上所有带病毒的网站都找出来。当然,谁都知道这是不可能的。小t却执意要完成这不能的任务,他说:“子子孙孙无穷匮也!”(愚公后继有人了)。 
    万事开头难,小t收集了好多病毒的特征码,又收集了一批诡异网站的源码,他想知道这些网站中哪些是有病毒的,又是带了怎样的病毒呢?顺便还想知道他到底收集了多少带病毒的网站。这时候他却不知道何从下手了。所以想请大家帮帮忙。小t又是个急性子哦,所以解决问题越快越好哦~~ 

    Input

    第一行,一个整数N(1<=N<=500),表示病毒特征码的个数。 
    接下来N行,每行表示一个病毒特征码,特征码字符串长度在20—200之间。 
    每个病毒都有一个编号,依此为1—N。 
    不同编号的病毒特征码不会相同。 
    在这之后一行,有一个整数M(1<=M<=1000),表示网站数。 
    接下来M行,每行表示一个网站源码,源码字符串长度在7000—10000之间。 
    每个网站都有一个编号,依此为1—M。 
    以上字符串中字符都是ASCII码可见字符(不包括回车)。 

    Output

    依次按如下格式输出按网站编号从小到大输出,带病毒的网站编号和包含病毒编号,每行一个含毒网站信息。 
    web 网站编号: 病毒编号 病毒编号 … 
    冒号后有一个空格,病毒编号按从小到大排列,两个病毒编号之间用一个空格隔开,如果一个网站包含病毒,病毒数不会超过3个。 
    最后一行输出统计信息,如下格式 
    total: 带病毒网站数 
    冒号后有一个空格。 

    Sample Input

    3
    aaa
    bbb
    ccc
    2
    aaabbbccc
    bbaacc

    Sample Output

    web 1: 1 2 3
    total: 1

    AC自动机模板题,需要注意的是用构造函数初始化节点会内存超限,至今还不清楚内部原因...   如果要初始化的话手动初始化节点即可。

    输入的字符是ASSIC中的可见字符,所以next数组开127就行。

    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #include<math.h>
    #include<queue>
    using namespace std;
    const int maxn=1e5+7;
    const int branch=129;
    queue<int> mmp;
    class AcTireNode
    {
    public:
    
        int next[branch];
        int id,flag;//0为初始值  id记录该病毒编号 0代表该节点不是病毒   
                    //如果该节点(及其后缀)被第k个文本串匹配的话就令flag=k 代表该节点已经被
                    //该字符串匹配过了,无需再匹配其后缀
          
        int fail;
        void clear()
        {
            fail=0;
            for(int i=0; i<branch; ++i)
                next[i]=-1;
            id=flag=0;
        }
    };
    class AcTire
    {
    private:
        AcTireNode *node;
        int top;
    public:
        AcTire()
        {
            node=new AcTireNode[maxn];
            node[0].clear();
            top=0;
        }
        int hash_letter(char c)
        {
            return (int)c;
        }
        void insert(char *s,int num)
        {
            int now=0;
            while(*s)
            {
                int i=hash_letter(*s);
                if(node[now].next[i]==-1)
                {
                    node[now].next[i]=++top;
                    node[top].clear();
                }
                now=node[now].next[i];
                ++s;
            }
            node[now].id=num;
        }
        void Bulid_fail()
        {
            int now,to;
            for(int i=0; i<branch; ++i)
            {
                if(node[0].next[i]!=-1)
                    mmp.push(node[0].next[i]);
            }
            while(!mmp.empty())
            {
                now=mmp.front();
                mmp.pop();
                for(int i=0; i<branch; ++i)
                {
                    if(node[now].next[i]!=-1)
                    {
                        mmp.push(node[now].next[i]);
                        to=node[now].fail;
                        while(to>0&&node[to].next[i]==-1)
                            to=node[to].fail;
                        if(node[to].next[i]!=-1)
                            to=node[to].next[i];
                        node[node[now].next[i]].fail=to;
                    }
                }
            }
        }
        void Find_tx(char *tx,int *val,int &cnt,int num)//返回匹配的病毒个数 以及病毒的编号
        {
            cnt=0;
            int now=0;
            int to;
            while(*tx)
            {
                int i=hash_letter(*tx);
                while(now>0&&node[now].next[i]==-1)
                    now=node[now].fail;
                if(node[now].next[i]!=-1)//有匹配的后缀
                {
                    now=node[now].next[i];
                    to=now;//开始找所有后缀匹配
                    while(to>0&&node[to].flag!=num)//这个节点 曾经是否被这个字符串 遍历过
                    {
                        if(node[to].id)
                            val[cnt++]=node[to].id;
                        node[to].flag=num;
                        to=node[to].fail;
                    }
                }
                ++tx;
            }
        }
        void clear()
        {
            top=0;
            node[0].clear();
        }
        ~AcTire()
        {
            delete []node;
        }
    };
    char tx[10010],str[210];
    int val[20];
    int main()
    {
        AcTire dch;
        int n,m,cnt,total;
        while(~scanf("%d",&n))
        {
            dch.clear();
            total=0;
            for(int i=1; i<=n; ++i)
            {
                scanf("%s",str);
                dch.insert(str,i);
            }
            dch.Bulid_fail();
            scanf("%d",&m);
            for(int i=1; i<=m; ++i)
            {
                scanf("%s",tx);
                dch.Find_tx(tx,val,cnt,i);
                if(cnt)
                {
                    ++total;
                    sort(val,val+cnt);
                    printf("web %d:",i);
                    for(int j=0; j<cnt; ++j)
                        printf(" %d",val[j]);
                    printf("
    ");
                }
            }
            printf("total: %d
    ",total);
        }
        return 0;
    }
  • 相关阅读:
    MySQL中删除重复数据只保留一条
    js 的try catch应用
    jQuery中on()方法用法实例
    js老生常谈之this,constructor ,prototype
    spring自定义标签之 规范定义XSD
    jquery jgrid filterToolBar beforeSearch 修改postData
    Guava学习笔记:Optional优雅的使用null
    Java之Collections.emptyList()、emptySet()、emptyMap()的作用和好处以及要注意的地方
    Druid SQL 解析器概览
    访问者模式
  • 原文地址:https://www.cnblogs.com/dchnzlh/p/10427294.html
Copyright © 2011-2022 走看看