ZOJ 3430 Detect the Virus 【AC自动机+解码】
解码的那些事儿,不多说。
注意解码后的结果各种情况都有,用整数数组存储,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自动机+解码】的更多相关文章
- ZOJ - 3430 Detect the Virus —— AC自动机、解码
题目链接:https://vjudge.net/problem/ZOJ-3430 Detect the Virus Time Limit: 2 Seconds Memory Limit: 6 ...
- zoj 3430 Detect the Virus(AC自己主动机)
题目连接:zoj 3430 Detect the Virus 题目大意:给定一个编码完的串,将每个字符相应着表的数值转换成6位二进制.然后以8为一个数值,又一次形成字符 串,推断给定询问串是否含有字符 ...
- ZOJ 3430 Detect the Virus
传送门: Detect the Virus ...
- ZOJ 3430 Detect the Virus(AC自动机)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3430 题意:给你n个编码后的模式串,和m个编码后的主串,求原来主 ...
- zoj 3430 Detect the Virus(AC自己主动机)
Detect the Virus Time Limit: 2 Seconds Memory Limit: 65536 KB One day, Nobita found that his co ...
- ZOJ 3430 Detect the Virus(AC自动机 + 模拟)题解
题意:问你主串有几种模式串.但是所有串都是加密的,先解码.解码过程为:先把串按照他给的映射表变成6位数二进制数,然后首尾衔接变成二进制长串,再8位8位取变成新的数,不够的补0.因为最多可能到255,所 ...
- ZOJ 3228 Searching the String(AC自动机)
Searching the String Time Limit: 7 Seconds Memory Limit: 129872 KB Little jay really hates to d ...
- ZOJ 4114 Detect the Virus(AC自动机)
Detect the Virus Time Limit: 2 Seconds Memory Limit: 65536 KB One day, Nobita found that his co ...
- ZOJ 3494 BCD Code(AC自动机 + 数位DP)题解
题意:每位十进制数都能转化为4位二进制数,比如9是1001,127是 000100100111,现在问你,在L到R(R <= $10^{200}$)范围内,有多少数字的二进制表达式不包含模式串. ...
随机推荐
- Linux字符设备中的两个重要结构体(file、inode)
对于Linux系统中,一般字符设备和驱动之间的函数调用关系如下图所示 上图描述了用户空间应用程序通过系统调用来调用程序的过程.一般而言在驱动程序的设计中,会关系 struct file 和 struc ...
- SQL转换函数to_char/to_date/to_number
日期型->字符型转换函数to_char(d [,fmt]) 函数to_char(d [,fmt])用于将日期型数值转换为字符串(varchar2类型),其中参数d用于指定日期值,fmt用于指定要 ...
- 常用元素的属性/方法 attr / val / html /text
常用元素的属性/方法 得到一个元素的高度, $("#myid").height() 得到一个元素的位置, $("#myid").offset() 返回的是一个o ...
- 服务 远程服务 AIDL 进程间通讯 IPC
Activity aidl接口文件 package com.bqt.aidlservice; interface IBinderInterface { /* 更改文件后缀为[.aidl]去掉 ...
- TreeGrid( 树形表格)
本节课重点了解 EasyUI 中 TreeGrid(树形表格)组件的使用方法,这个组件依赖于DataGrid(数据表格)组件 一. 加载方式//建立一个 JSON 文件[{"id" ...
- ValidateBox( 验证框) 组件
本节课重点了解 EasyUI 中 ValidateBox(验证框)组件的使用方法,这个组件依赖于Tooltip(提示框)组件. 一. 加载方式//class 加载方式<input id=&quo ...
- .net对文件的操作之文件读写
读写文件的步骤一般需要5步: 创建文件流 创建读写器 执行读或写的操作 关闭读写器 关闭文件流 需要引用:System.IO这个命名空间 代码演示: string path = @"F:\a ...
- CSS背景特殊属性值
CSS代码示例-背景附着属性(background-attachment)-[背景图固定不动,不跟随滚动条滚动]:<html><head><title>背景附着属性 ...
- 小技巧之jQueryMobile
使用JqueryMobile+MVC做一个手机网站,也有2个月了.有一些小小的经验,跟大伙们分享一下下 小技巧1: 禁用所有Ajax加载,它会很烦人的. $.mobile.ajaxLinksEnabl ...
- arcgis server manager - An error has occured on the server. For details please check the Event (Application) log on the web.
当登陆 Arcgis Server Manager的时候,点击 "Services" 中的选项"Manage Services",就报错: An error h ...