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}$)范围内,有多少数字的二进制表达式不包含模式串. ...
随机推荐
- jquery之前后台交互
//js文件 function getMinatoSingleGoodsCategorys(type){ $("#"+type+"MinatoGoodsCategoryI ...
- OD: Memory Attach Technology - Off by One, Virtual Function in C++ & Heap Spray
Off by One 根据 Halvar Flake 在“Third Generation Exploitation”中的描述,漏洞利用技术依攻击难度从小到大分为三类: . 基础的栈溢出利用,可以利用 ...
- poj2409
用n个颜色的珠子编项链,求有多少种情况 由N(G,C) = 所有f的稳定核的和/|G| m边形有m种旋转m种翻转 首先说旋转,有模线性方程可知每种旋转都有gcd(m,i)个循环节且每个循环节长度为n/ ...
- Visual Studio发布项目到远程服务器的步骤
第一步: 需要远程服务器上安装Web Deploy ,下载地址:http://www.iis.net/downloads/microsoft/web-deploy PS.安装时选择完全安装. 第二步: ...
- 复习css3的部分属性
利用css3实现一个半月形的西瓜,利用queue实现吃西瓜的动画.练习例子 css3: 1.圆角 border-radius:100%; border-top-left-radius:5px; 2.变 ...
- Css3抖动
http://files.cnblogs.com/xinlinux/csshake.min.css <div class="shake">AAA</div> ...
- 40条优化php代码的小实例
1.如果一个方法能被静态,那就声明他为静态的,速度可提高1/4; 2.echo的效率高于print,因为echo没有返回值,print返回一个整型; 3.在循环之前设置循环的最大次数,而非在在循环中; ...
- python之6-1常用函数
1.休眠函数 import time time.sleep(n) n可以是整数或者小数,单位是秒 2.打开文件函数 open('n','m',k) n是文件路径,如果只有文件名,则是py程序所在文件夹 ...
- 性能测试 - some
1.日常业务中测试过的最大并发数,其QPS为多少? 答: 从服务端开发处得知线上某台机器单接口访问量为63万 因该接口为视频类访问接口,大多数接口集中于晚间时段.可计算QPS = 63万/8/3600 ...
- Java添加自定义注解
今天在整合MyBatis过程中,我使用扫描的方式去扫描DAO目录下的Java文件,但是在Service层使用Autowired的时候报错,但是工程能正常运行,此处有Bug! 解决方案: 1.创建 an ...