解码的那些事儿,不多说。

注意解码后的结果各种情况都有,用整数数组存储,char数组会超char类型的范围(这个事最蛋疼的啊)建立自动机的时候不能用0来判断结束。

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <string>
#include <iostream>
using namespace std;
vector<int> ans; struct AC_Automata {
#define N 60010
#define M 256
int ch[N][M], sz;
int val[N], last[N], f[N], cc[N]; void clear() {
sz = 1;
memset(ch[0], 0, sizeof(ch[0]));
} void insert(int s[], int l, int v) {
int u = 0;
for (int i=0; i<l; i++) {
int c = s[i];
if (!ch[u][c]) {
memset(ch[sz], 0, sizeof(ch[sz]));
val[sz] = 0;
ch[u][c] = sz++;
}
u = ch[u][c];
}
val[u] = v;
}
void build() {
queue<int> q;
f[0] = 0;
for (int c=0; c<M; c++) {
int u = ch[0][c];
if (u) { f[u] = last[u] = 0; q.push(u); }
}
while (!q.empty()) {
int r = q.front(); q.pop();
for (int c=0; c<M; c++) {
int u = ch[r][c];
if (!u) { ch[r][c] = ch[f[r]][c]; continue; }
q.push(u);
f[u] = ch[f[r]][c];
last[u] = val[f[u]] ? f[u] : last[f[u]];
}
}
}
void find(int s[], int l) {
int j = 0;
for (int i=0; i<l; i++) {
int c = s[i];
j = ch[j][c]; if (val[j]) print(j);
else if (last[j]) print(last[j]);
}
}
void print(int j) {
if (j) {
ans.push_back(val[j]);
print(last[j]);
}
}
} ac;
int get(char c) {
if (c >= 'A' && c <= 'Z') return c-'A';
if (c >= 'a' && c <= 'z') return c-'a'+26;
if (c >= '0' && c <= '9') return c-'0'+ 52;
if (c == '+') return 62;
return 63;
} int ss[30002];
int bit[30003*10]; int decode(char str[]) {
int top = 0;
int a[10]; for(int i=0; str[i]; i++) {
if(str[i]!='='){
int x = get(str[i]);
for(int j=5;j>=0;j--){
a[j] = x & 1;
x >>= 1;
}
for (int j=0; j<6; j++) bit[top++] = a[j];
}else
top -= 2;
} int x = 0;
int tot = 0;
for(int i=0;i<top;){
x = 0;
for(int j=0;j<8;j++)
x = x<<1 | bit[i++];
ss[tot++] = x;
}
return tot;
} int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif int n, m;
char s[30023]; while (scanf(" %d", &n) == 1) {
ac.clear();
for (int i=1; i<=n; i++) {
scanf(" %s", s);
int l = decode(s);
ac.insert(ss, l, i);
}
ac.build(); scanf(" %d", &m);
while (m--) {
ans.clear();
scanf(" %s", s);
int l = decode(s);
ac.find(ss, l); sort(ans.begin(), ans.end());
int cnt = unique(ans.begin(), ans.end()) - ans.begin();
printf("%d\n", cnt);
} puts("");
} return 0;
}

ZOJ 3430 Detect the Virus 【AC自动机+解码】的更多相关文章

  1. ZOJ - 3430 Detect the Virus —— AC自动机、解码

    题目链接:https://vjudge.net/problem/ZOJ-3430 Detect the Virus Time Limit: 2 Seconds      Memory Limit: 6 ...

  2. zoj 3430 Detect the Virus(AC自己主动机)

    题目连接:zoj 3430 Detect the Virus 题目大意:给定一个编码完的串,将每个字符相应着表的数值转换成6位二进制.然后以8为一个数值,又一次形成字符 串,推断给定询问串是否含有字符 ...

  3. ZOJ 3430 Detect the Virus

    传送门: Detect the Virus                                                                                ...

  4. ZOJ 3430 Detect the Virus(AC自动机)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3430 题意:给你n个编码后的模式串,和m个编码后的主串,求原来主 ...

  5. zoj 3430 Detect the Virus(AC自己主动机)

    Detect the Virus Time Limit: 2 Seconds      Memory Limit: 65536 KB One day, Nobita found that his co ...

  6. ZOJ 3430 Detect the Virus(AC自动机 + 模拟)题解

    题意:问你主串有几种模式串.但是所有串都是加密的,先解码.解码过程为:先把串按照他给的映射表变成6位数二进制数,然后首尾衔接变成二进制长串,再8位8位取变成新的数,不够的补0.因为最多可能到255,所 ...

  7. ZOJ 3228 Searching the String(AC自动机)

    Searching the String Time Limit: 7 Seconds      Memory Limit: 129872 KB Little jay really hates to d ...

  8. ZOJ 4114 Detect the Virus(AC自动机)

    Detect the Virus Time Limit: 2 Seconds      Memory Limit: 65536 KB One day, Nobita found that his co ...

  9. ZOJ 3494 BCD Code(AC自动机 + 数位DP)题解

    题意:每位十进制数都能转化为4位二进制数,比如9是1001,127是 000100100111,现在问你,在L到R(R <= $10^{200}$)范围内,有多少数字的二进制表达式不包含模式串. ...

随机推荐

  1. Spring学习笔记——Spring中的BeanFactory与FactoryBean

    BeanFactory BeanFactory是Spring的org.springframework.beans.factory下的一个接口,是Spring IOC所遵守的基本编程规范.他的实现类有D ...

  2. java.lang.NoSuchMethodError: org.apache.commons.io.FileUtils.getTempDirectory()Ljava/io/File;

    我出现这个问题的原因是使用ueditor上传图片 如果不是commons.io的jar包缺失,就是jar包有冲突 另外:最新的ueditor(1.4.3.1)使用的是commons-io-2.4.ja ...

  3. 你的sscanf用对了吗

    用sscanf解析输入字符串 我们平常编写的很多应用程序都会处理各种各样的输入,这些输入或来自本地文件,或来自网络,或来自用户的输入.今天,让我们来看看sscanf这个和字符串相关的函数可能给你带来的 ...

  4. ssh免密码登陆远程服务器

    ssh免密码登陆远程服务器 在使用windows下的cygwin或者在linux下使用Terminal进行远程服务器登陆测试的时候总是会要求输入账号密码,对于此我们可以使用ssh将公钥放在服务器上的方 ...

  5. JavaScript“闭包”精解

    一.变量的作用域 要理解闭包,首先必须理解Javascript特殊的变量作用域. 变量的作用域无非就是两种:全局变量和局部变量. 详细了解 Javascript语言的特殊之处,就在于函数内部可以直接读 ...

  6. Java NIO框架Netty教程(一) – Hello Netty

    先啰嗦两句,如果你还不知道Netty是做什么的能做什么.那可以先简单的搜索了解一下.我只能说Netty是一个NIO的框架,可以用于开发分布式的Java程序.具体能做什么,各位可以尽量发挥想象.技术,是 ...

  7. hibernate 核心总结 (面试)

    1:1(类与类之间) husband----wife 外键关联: a)单向@OneToOne b)双向@OneToOne, mappedby="husband" --------- ...

  8. JQ 遍历节点

    .children() : 取得匹配元素的子元素集合 .next() :取得匹配元素后面紧邻的同辈元素 .prev() :取得匹配元素前面紧邻的同辈元素 .siblings() :取得匹配元素前.后的 ...

  9. uva 11536 - Smallest Sub-Array

    题目大意:按照题目中的要求构造出一个序列,找出最短的子序列,包含1~k. 解题思路:先根据题目的方法构造出序列,然后用Towpointer的方法,用v[i]来记录当前[l, r]中有几个i:当r移动时 ...

  10. Apache之AllowOverride参数详解

    通常利用Apache的rewrite模块对 URL 进行重写的时候, rewrite规则会写在 .htaccess 文件里.但要使 apache 能够正常的读取.htaccess 文件的内容,就必须对 ...