zoukankan      html  css  js  c++  java
  • [HDU2896]病毒侵袭(AC自动机)

    传送门

    题目中文描述,赞!

    除了val记录id以外就是模板。

    注意:每次数组都要清0.0

    ——代码

     1 #include <cstdio>
     2 #include <queue>
     3 #include <cstring>
     4 #include <algorithm>
     5 
     6 using namespace std;
     7 
     8 int n, m, sz, tot;
     9 int ch[100005][130], val[100005], ans[100005], fail[100005];
    10 bool vis[100005];
    11 char s[100005];
    12 
    13 inline void insert(int id)
    14 {
    15     int i, x, now = 0, len = strlen(s);
    16     for(i = 0; i < len; i++)
    17     {
    18         x = s[i];
    19         if(!ch[now][x]) ch[now][x] = ++sz;
    20         now = ch[now][x];
    21     }
    22     val[now] = id;
    23 }
    24 
    25 inline void make_fail()
    26 {
    27     int i, now;
    28     queue <int> q;
    29     for(i = 0; i <= 128; i++)
    30      if(ch[0][i])
    31       q.push(ch[0][i]);
    32     while(!q.empty())
    33     {
    34         now = q.front(), q.pop();
    35         for(i = 0; i <= 128; i++)
    36         {
    37             if(!ch[now][i])
    38             {
    39                 ch[now][i] = ch[fail[now]][i];
    40                 continue;
    41             }
    42             fail[ch[now][i]] = ch[fail[now]][i];
    43             q.push(ch[now][i]);
    44         }
    45     }
    46 }
    47 
    48 inline void ac()
    49 {
    50     int i, now = 0, x, y, len = strlen(s);
    51     for(i = 0; i < len; i++)
    52     {
    53         vis[now] = 1;
    54         x = s[i];
    55         y = ch[now][x];
    56         while(y && !vis[y])
    57         {
    58             vis[y] = 1;
    59             if(val[y]) ans[++ans[0]] = val[y];
    60             y = fail[y];
    61         }
    62         now = ch[now][x];
    63     }
    64 }
    65 
    66 int main()
    67 {
    68     int i, j;
    69     scanf("%d", &n);
    70     for(i = 1; i <= n; i++)
    71     {
    72         scanf("%s", s);
    73         insert(i);
    74     }
    75     make_fail();
    76     scanf("%d", &m);
    77     for(i = 1; i <= m; i++)
    78     {
    79         scanf("%s", s);
    80         memset(vis, 0, sizeof(vis));
    81         memset(ans, 0, sizeof(ans));
    82         ac();
    83         if(ans[0])
    84         {
    85             printf("web %d:", i);
    86             sort(ans + 1, ans + ans[0] + 1);
    87             for(j = 1; j <= ans[0]; j++) printf(" %d", ans[j]);
    88             printf("
    ");
    89             tot++;
    90         }
    91     }
    92     printf("total: %d
    ", tot);
    93     return 0;
    94 }
    View Code
  • 相关阅读:
    CF110A Nearly Lucky Number
    Max Sum Plus Plus HDU – 1024
    洛谷 p1003 铺地毯
    poj-1226
    Where is the Marble? UVA – 10474
    Read N Characters Given Read4
    Guess Number Higher or Lower && 九章二分法模板
    Intersection of Two Arrays II
    Reverse Vowels of a String
    Meeting Rooms
  • 原文地址:https://www.cnblogs.com/zhenghaotian/p/6753505.html
Copyright © 2011-2022 走看看