zoukankan      html  css  js  c++  java
  • 【ZOJ】3430 Detect the Virus

      1 #include<cstdio>
      2 #include<cstring>
      3 #include<vector>
      4 #include<queue>
      5 #define MAXL 256
      6 #define MAXN 50010
      7 using namespace std;
      8 char str[MAXN];
      9 int dg[MAXN], code[MAXN], size;
     10 bool vis[MAXN];
     11 struct node {
     12     int fail, cnt, next[MAXL];
     13     void Init() {
     14         fail = cnt = 0;
     15         memset(next, 0, sizeof(next));
     16     }
     17 };
     18 node tree[MAXN];
     19 inline int GET(char ch) {
     20     if (ch >= 'A' && ch <= 'Z')
     21         return ch - 'A';
     22     if (ch >= 'a' && ch <= 'z')
     23         return ch - 'a' + 26;
     24     if (ch >= '0' && ch <= '9')
     25         return ch - '0' + 52;
     26     if (ch == '+')
     27         return 62;
     28     return 63;
     29 }
     30 void DoIt() {
     31     int len, i, j, k, g;
     32     vector<int> t;
     33     memset(dg, 0, sizeof(dg));
     34     for (len = strlen(str); str[len - 1] == '='; len--)
     35         ;
     36     str[len] = 0;
     37     for (i = 0; i < len; i++)
     38         t.push_back(GET(str[i]));
     39     for (i = 0; i < t.size(); i++) {
     40         for (j = 6 * (i + 1) - 1; j >= 6 * i; j--) {
     41             if (t[i] & 1)
     42                 dg[j] = 1;
     43             t[i] >>= 1;
     44         }
     45     }
     46     k = t.size() * 6 / 8;
     47     for (i = 0; i < k; i++) {
     48         for (g = 0, j = 8 * i; j < 8 * (i + 1); j++)
     49             g = (g << 1) + dg[j];
     50         code[i] = g;
     51     }
     52     code[i] = -1;
     53 }
     54 void Insert() {
     55     int now, t, i;
     56     for (now = i = 0; code[i] >= 0; i++) {
     57         t = code[i];
     58         if (!tree[now].next[t]) {
     59             tree[++size].Init();
     60             tree[now].next[t] = size;
     61         }
     62         now = tree[now].next[t];
     63     }
     64     tree[now].cnt++;
     65 }
     66 void BFS() {
     67     int i, now, p;
     68     queue<int> q;
     69     q.push(0);
     70     while (!q.empty()) {
     71         now = q.front();
     72         q.pop();
     73         for (i = 0; i < MAXL; i++) {
     74             if (tree[now].next[i]) {
     75                 p = tree[now].next[i];
     76                 if (now)
     77                     tree[p].fail = tree[tree[now].fail].next[i];
     78                 q.push(p);
     79             } else
     80                 tree[now].next[i] = tree[tree[now].fail].next[i];
     81         }
     82     }
     83 }
     84 int Match() {
     85     int now, i, t, p, ans;
     86     for (ans = now = i = 0; code[i] >= 0; i++) {
     87         t = code[i];
     88         now = tree[now].next[t];
     89         for (p = now; p; p = tree[p].fail) {
     90             if (!vis[p]) {
     91                 ans += tree[p].cnt;
     92                 vis[p] = true;
     93             }
     94         }
     95     }
     96     return ans;
     97 }
     98 int main() {
     99     int n, i, q, ans;
    100     while (~scanf(" %d", &n)) {
    101         tree[0].Init();
    102         for (i = size = 0; i < n; i++) {
    103             scanf(" %s", str);
    104             DoIt();
    105             Insert();
    106         }
    107         BFS();
    108         scanf("%d", &q);
    109         while (q--) {
    110             memset(vis, false, sizeof(vis));
    111             scanf(" %s", str);
    112             DoIt();
    113             ans = Match();
    114             printf("%d\n", ans);
    115         }
    116         putchar('\n');
    117     }
    118     return 0;
    119 }
  • 相关阅读:
    grid列的值格式化
    页面记载给绑定query的grid加filter
    页面加载后从后面带数据到前台
    waf2控件名
    通讯框架选型
    C# 访问修饰符和const、readonly
    ZooKeeper典型应用场景一览
    ZooKeeper典型使用场景一览
    摘的一段关于原型的介绍
    D3.js和three.js
  • 原文地址:https://www.cnblogs.com/DrunBee/p/2622351.html
Copyright © 2011-2022 走看看