[kuangbin带你飞]专题17:D - Detect the Virus(ZOJ3430)

原题

AC自动机模版 + base64的解码

wa1 : 同个匹配串内多次出现的病毒只记录一次。
wa2 : 范围为256的部分特殊字符char的强制类型转换有问题(不确定)
解决:得到ascll码的int数组, 把int型数组作为匹配串直接去匹配

#include<iostream>
#include<stack>
#include<queue>
#include<string>
#include<cstring>
#include<cstdio>
#include<vector>
using namespace std;

const int MAXN = 50010;

int map(char c) {
    if(c >= 'A' && c <= 'Z') return c - 'A';
    else if(c >= 'a' && c <= 'z') return  c - 'a' + 26;
    else if(c >= '0' && c <= '9') return c - '0' + 52;
    else if(c == '+') return 62;
    else return 63;
}

int ascll[MAXN];

int change(string s) {
    //init
    queue<int> q;   
    memset(ascll, 0, sizeof(ascll));

    int l = (int)s.length();
    for(int i = 0; i < l && s[i] != '='; i++) {
        int temp = map(s[i]);
        for(int i = 5; i >= 0; i--) q.push(temp>>i & 1);
    }
    int k = 0;
    while(q.size() >= 8) {//8个一取,小于8时即代表剩下的都是添加的0
        for(int i = 7; i >= 0; i--) {
            if(q.front()) ascll[k] += 1<<i;
            q.pop();
        }
        k++;
    }
    ascll[k] = -1;
    return k;
}


int node[MAXN][256], fail[MAXN], End[MAXN];
int root, L;
bool vis[MAXN];
int newnode() {
    for(int i = 0; i < 256; i++) node[L][i] = -1;
    fail[L] = -1;
    End[L] = 0;
    L++;
    return L-1;
}

void init() {
    L = 0;
    root = newnode();
}

void insert(int l) {
    int now = root;
    for(int i = 0; i < l; i++) {
        int id = ascll[i];
        if(node[now][id] == -1) node[now][id] = newnode();
        now = node[now][id];
    }
    End[now]++;
}

void setfail() {
    queue<int> q;
    q.push(root);
    while(!q.empty()) {
        int now = q.front(); q.pop();
        for(int i = 0 ; i < 256; i++) if(node[now][i] != -1) {
            int pre = fail[now];
            while(pre != -1 && node[pre][i] == -1) pre = fail[pre];
            fail[node[now][i]] = pre  == -1 ? root : node[pre][i];
            q.push(node[now][i]);
        }
    }

}
int query(int l) {
    int now = root;
    int cnt = 0;
    for(int i = 0; i < l; i++) {
        int id = ascll[i];
        while(now != -1 && node[now][id] == -1) now = fail[now];
        now = now == -1 ? root : node[now][id];
        for(int p = now; p != -1; p = fail[p]) if(End[p] && !vis[p]) {
            cnt += End[p];
            vis[p] = true;
        }
    }
    return cnt;
}

int main() {
    freopen("a.in", "r", stdin);
    ios::sync_with_stdio(false);
    int n;
    string s;
    while(cin >> n) {
        init();
        for(int i = 1; i <= n; i++) {
            cin >> s; insert(change(s));
        }
        setfail();
        cin >> n;
        while(n--) {
            memset(vis, false, sizeof(vis));
            cin >> s; cout << query(change(s)) << endl;
        }
        cout << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值