• HDU2896 病毒侵袭 —— AC自动机


    题目链接:https://vjudge.net/problem/HDU-2896

    病毒侵袭

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 31288    Accepted Submission(s): 7137


    Problem Description
    当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻。。。。在这样的时刻,人们却异常兴奋——我们能在有生之年看到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
     
    Source
     
    Recommend
    gaojie

    题解:

    1.将每个病毒加入Trie树中,并记录其编号。

    2.对于每个网站,将其字符串与Trie树进行匹配。记录其含有的病毒及个数。

    代码如下:

      1 #include<bits/stdc++.h>
      2 using namespace std;
      3 typedef long long LL;
      4 const double EPS = 1e-6;
      5 const int INF = 2e9;
      6 const LL LNF = 9e18;
      7 const int MOD = 1e9+7;
      8 const int MAXN = 1e5+10;
      9 
     10 //id为当前病毒的编号,tot为当前网站所含的病毒数,存于virus[]
     11 int id, tot, virus[5];
     12 struct Trie
     13 {
     14     const static int sz = 128;  //ASCII码最大为128,直接用字符值作为下标即可
     15     int next[MAXN][sz], fail[MAXN], end[MAXN];
     16     int root, L;
     17 
     18     int newnode()
     19     {
     20         for(int i = 0; i<sz; i++)
     21             next[L][i] = -1;
     22         end[L++] = 0;
     23         return L-1;
     24     }
     25 
     26     void init()
     27     {
     28         L = 0;
     29         root = newnode();
     30     }
     31 
     32     void insert(char buf[])
     33     {
     34         int len = strlen(buf);
     35         int now = root;
     36         for(int i = 0; i<len; i++)
     37         {
     38             if(next[now][buf[i]] == -1)
     39                 next[now][buf[i]] = newnode();
     40             now = next[now][buf[i]];
     41         }
     42         end[now] = id;  //记录病毒的编号
     43     }
     44 
     45     void build()
     46     {
     47         queue<int>Q;
     48         fail[root] = root;
     49         for(int i = 0; i<sz; i++)
     50         {
     51             if(next[root][i] == -1)
     52                 next[root][i] = root;
     53             else
     54                 fail[next[root][i]] = root, Q.push(next[root][i]);
     55         }
     56         while(!Q.empty())
     57         {
     58             int now = Q.front();
     59             Q.pop();
     60             for(int i = 0; i<sz; i++)
     61             {
     62                 if(next[now][i] == -1)
     63                     next[now][i] = next[fail[now]][i];
     64                 else
     65                     fail[next[now][i]] = next[fail[now]][i], Q.push(next[now][i]);
     66             }
     67         }
     68     }
     69 
     70     void query(char buf[])
     71     {
     72         int len = strlen(buf);
     73         int now = root;
     74         for(int i = 0; i<len; i++)
     75         {
     76             now = next[now][buf[i]];
     77             int tmp = now;
     78             while(tmp != root)
     79             {
     80                 if(end[tmp])
     81                 {
     82                     //在此病毒发现新的病毒
     83                     if(tot==0||(tot==1 && virus[1]!=end[tmp]) ||
     84                        (tot==2 && virus[1]!=end[tmp] && virus[2]!=end[tmp]))
     85                     virus[++tot] = end[tmp];
     86                 }
     87                 tmp = fail[tmp];
     88             }
     89         }
     90     }
     91 };
     92 
     93 Trie ac;
     94 char buf[MAXN];
     95 int main()
     96 {
     97     int n, m;
     98     while(scanf("%d", &n)!=EOF)
     99     {
    100         ac.init();
    101         for(int i = 1; i<=n; i++)
    102         {
    103             id = i;
    104             scanf("%s",buf);
    105             ac.insert(buf);
    106         }
    107         ac.build();
    108 
    109         scanf("%d", &m);
    110         int sum = 0;
    111         for(int i = 1; i<=m; i++)
    112         {
    113             tot = 0;
    114             scanf("%s",buf);
    115             ac.query(buf);
    116             if(tot)
    117             {
    118                 sum++;
    119                 printf("web %d:", i);
    120                 sort(virus+1, virus+1+tot);
    121                 for(int j = 1; j<=tot; j++)
    122                     printf(" %d", virus[j]);
    123                 printf("
    ");
    124             }
    125         }
    126         printf("total: %d
    ", sum);
    127     }
    128 }
    View Code
  • 相关阅读:
    vmware centos .net core sdk开发测试
    Unable to bind to http://localhost:8080 on the IPv6 loopback interface: 'Cannot assign requested address'.
    把bootstrap4 dropdown 的导航下拉菜单触发方式改为鼠标浮动触发
    vs项目同步到gitee方法
    PetaPoco轻量级ORM框架
    本田汽车车机大屏破解教程
    SqlServer 游标逐行更新数据,根据上一行的数据来更新当前行
    Angular 2/4/5+ 重复点击菜单刷新界面
    Ionic 2 + 手动搭建开发环境教程 【转】
    在过去五分钟内,TypeScript语言服务以外终止了5次
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7856049.html
Copyright © 2020-2023  润新知