• hdu 2896 病毒侵袭


    http://acm.hdu.edu.cn/showproblem.php?pid=2896

      AC自动机的简单题。。。忘记关debug了,wa了一次。。。囧!

    View Code
      1 #include <cstdio>
      2 #include <cstring>
      3 #include <algorithm>
      4 #include <set>
      5 #include <cstdlib>
      6 
      7 using namespace std;
      8 
      9 const int kind = 128;
     10 const int maxn = 201 * 501;
     11 
     12 struct Node{
     13     int fail;
     14     int next[kind];
     15     int count;
     16     void init(){
     17         fail = -1;
     18         count = 0;
     19         memset(next, -1, sizeof(next));
     20     }
     21 }node[maxn];
     22 
     23 char keyword[201];
     24 char str[10001];
     25 int head, tail, nodeUsed;
     26 int q[maxn];
     27 
     28 void insert(char *s, int root, int num){
     29     int p = root;
     30     int i = 0, index;
     31 
     32     while (s[i]){
     33         index = s[i] - 33;
     34         if (node[p].next[index] == -1){
     35             node[p].next[index] = nodeUsed;
     36             node[nodeUsed].init();
     37             nodeUsed++;
     38         }
     39         p = node[p].next[index];
     40         i++;
     41     }
     42     node[p].count = num;
     43 }
     44 
     45 void build(int root){
     46     int i;
     47 
     48     node[root].fail = -1;
     49     head = tail = 0;
     50     q[head++] = root;
     51     while (head != tail){
     52         int temp = q[tail++];
     53         int p = -1;
     54 
     55         for (i = 0; i < kind; i++){
     56             if (node[temp].next[i] != -1){
     57                 if (temp == root) node[node[temp].next[i]].fail = root;
     58                 else{
     59                     p = node[temp].fail;
     60                     while (p != -1){
     61                         if (node[p].next[i] != -1){
     62                             node[node[temp].next[i]].fail = node[p].next[i];
     63                             break;
     64                         }
     65                         p = node[p].fail;
     66                     }
     67                     if (p == -1) node[node[temp].next[i]].fail = root;
     68                 }
     69                 q[head++] = node[temp].next[i];
     70             }
     71         }
     72     }
     73 }
     74 
     75 set<int> virus;
     76 
     77 void query(int root){
     78     int i = 0, index;
     79     int p = root;
     80 
     81     virus.clear();
     82     while (str[i]){
     83         index = str[i] - 33;
     84         while (node[p].next[index] == -1 && p != root) p = node[p].fail;
     85         p = node[p].next[index];
     86         p = (p == -1) ? root : p;
     87 
     88         if (node[p].count) virus.insert(node[p].count);
     89         i++;
     90     }
     91 }
     92 
     93 void traverse(int root){
     94     if (root == -1) return ;
     95     printf("root %d\n", root);
     96     printf("fail %d\n", node[root].fail);
     97     printf("count %d\n", node[root].count);
     98     for (int i = 0; i < kind; i++){
     99         if (node[root].next[i] == -1) continue;
    100         printf("%c %d\n", i + 'a', node[root].next[i]);
    101     }
    102     puts("");
    103     for (int i = 0; i < kind; i++){
    104         traverse(node[root].next[i]);
    105     }
    106 }
    107 
    108 int main(){
    109     int n;
    110 
    111 //freopen("in", "r", stdin);
    112     while (~scanf("%d", &n)){
    113         int root = 0;
    114 
    115         node[0].init();
    116         nodeUsed = 1;
    117 
    118         for(int i = 1; i <= n; i++){
    119             scanf("%s", keyword);
    120             insert(keyword, root, i);
    121         }
    122         build(root);
    123 //traverse(root);
    124 //puts("Built!");
    125         scanf ("%d", &n);
    126 
    127         int cnt = 0;
    128 
    129         for (int i = 1; i <= n; i++){
    130             scanf("%s", str);
    131             query(root);
    132 
    133             if (virus.size()){
    134                 cnt++;
    135                 printf("web %d:", i);
    136                 for (set<int>::iterator ii = virus.begin(); ii != virus.end(); ii++){
    137                     printf(" %d", *ii);
    138                 }
    139                 puts("");
    140             }
    141         }
    142         printf("total: %d\n", cnt);
    143     }
    144 
    145     return 0;
    146 }

    ——written by Lyon

  • 相关阅读:
    FTP文件乱码和传输模式解释
    win7(x64)+VS2012+cocos2d-x环境的配置以及试运行
    XCode详解及iOSApp上传
    WP8教程
    Windows Phone 8 开发环境搭建
    Wp8开发环境搭建总结
    跟随玩家
    JniHelper 含安卓推送
    软硬结合第二篇——酷我音乐盒的逆天玩法
    IOS ITunesConnect 修改开发商名称
  • 原文地址:https://www.cnblogs.com/LyonLys/p/hdu_2896_Lyon.html
Copyright © 2020-2023  润新知