题目连接:zoj 3430 Detect the Virus

题目大意:给定一个编码完的串,将每个字符相应着表的数值转换成6位二进制。然后以8为一个数值,又一次形成字符

串,推断给定询问串是否含有字符集中的串。

解题思路:主要是题意,逆编码部分注意。转换完了之后,可能有字符'\0'。所以不能用字符串的形式储存。要用int型

的数组。

注意有同样串的可能。

#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm> using namespace std; typedef long long ll; const int maxn = 512 * 100;
const int sigma_size = 256; struct Aho_Corasick {
int sz, g[maxn][sigma_size];
int tag[maxn], fail[maxn], last[maxn];
bool vis[520];
int jump[520]; void init();
int idx(char ch);
void insert(int* str, int k);
void getFail();
int match(int* str);
void put(int u);
}A; inline int change(char ch) {
if (ch >= 'A' && ch <= 'Z')
return ch - 'A';
if (ch >= 'a' && ch <= 'z')
return ch - 'a' + 26;
if (ch >= '0' && ch <= '9')
return ch - '0' + 52;
if (ch == '+')
return 62;
return 63;
} const int maxl = 5005; char w[maxl];
int s[maxl * 2]; void input(int* s) { scanf("%s", w);
s[0]=0;
int n = strlen(w);
while (n && w[n-1] == '=') n--;
w[n] = 0;
for(int i=0,len=0,x=0;w[i];++i){
len+=6,x=(x<<6)|change(w[i]);
if(len>=8){
s[++s[0]]=(x>>(len-8))&0xff;
len-=8;
}
} /*
for(int i = 1; i <= s[0]; i++)
printf(" %d", s[i]);
printf("\n");
*/
} int N, M; int main () {
while (scanf("%d", &N) == 1) {
A.init(); for (int i = 1; i <= N; i++) {
input(s);
A.insert(s, i);
}
A.getFail(); scanf("%d", &M);
for (int i = 1; i <= M; i++) {
input(s);
printf("%d\n", A.match(s));
}
printf("\n");
}
return 0;
} void Aho_Corasick::init() {
sz = 1;
tag[0] = 0;
memset(g[0], 0, sizeof(g[0]));
} int Aho_Corasick::idx(char ch) {
return ch;
} void Aho_Corasick::put(int u) {
int p = tag[u];
while (p && vis[p] == 0) {
vis[p] = 1;
p = jump[p];
}
if (last[u])
put(last[u]);
} void Aho_Corasick::insert(int* str, int k) {
int u = 0; for (int i = 1; i <= str[0]; i++) {
int v = str[i];
while (v >= sigma_size);
if (g[u][v] == 0) {
tag[sz] = 0;
memset(g[sz], 0, sizeof(g[sz]));
g[u][v] = sz++;
}
u = g[u][v];
} jump[k] = tag[u];
tag[u] = k;
} int Aho_Corasick::match(int* str) {
memset(vis, 0, sizeof(vis));
int u = 0; for (int i = 1; i <= str[0]; i++) {
int v = str[i];
while (v >= sigma_size);
while (u && g[u][v] == 0)
u = fail[u]; u = g[u][v]; if (tag[u])
put(u);
else if (last[u])
put(last[u]);
} int ret = 0;
for (int i = 1; i <= N; i++)
if (vis[i])
ret++;
return ret;
} void Aho_Corasick::getFail() {
queue<int> que; for (int i = 0; i < sigma_size; i++) {
int u = g[0][i];
if (u) {
fail[u] = last[u] = 0;
que.push(u);
}
} while (!que.empty()) {
int r = que.front();
que.pop(); for (int i = 0; i < sigma_size; i++) {
int u = g[r][i]; if (u == 0) {
g[r][i] = g[fail[r]][i];
continue;
} que.push(u);
int v = fail[r];
while (v && g[v][i] == 0)
v = fail[v]; fail[u] = g[v][i];
last[u] = tag[fail[u]] ? fail[u] : last[fail[u]];
}
}
}

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自己主动机)

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

  3. ZOJ 3430 Detect the Virus

    传送门: Detect the Virus                                                                                ...

  4. ZOJ - 3228 Searching the String (AC自己主动机)

    Description Little jay really hates to deal with string. But moondy likes it very much, and she's so ...

  5. ZOJ 3494 BCD Code (AC自己主动机 + 数位DP)

    题目链接:BCD Code 解析:n个病毒串.问给定区间上有多少个转换成BCD码后不包括病毒串的数. 很奇妙的题目. . 经典的 AC自己主动机 + 数位DP 的题目. 首先使用AC自己主动机,得到b ...

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

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

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

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

  8. ZOJ 3430 Detect the Virus 【AC自动机+解码】

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

  9. Zoj 3545 Rescue the Rabbit(ac自己主动机+dp)

    标题效果: 鉴于DNA有一个正确的顺序值.请构造一个长度I的DNA在这个序列使DNA正确的顺序值极大.它被认为是负的输出噼啪. .. IDEAS: 施工顺序是,ac己主动机上走,求最大要用到dp dp ...

随机推荐

  1. 第1张 Maven简介 学习笔记

    什么是构建? 编译.运行单元测试.生成文档.打包和部署 Maven的应用: 构建工具 依赖管理工具 通过坐标系统定位到每一个构建(artifact) 项目信息管理工具 Maven对于项目目录结构.测试 ...

  2. AirPlay、DLNA、Miracast三大无线技术介绍

    小米盒子之AirPlay.DLNA.Miracast三大无线技术介绍 米官方称小米盒子的米联功能可以将小米手机或iPhone.iPad上的图片.音乐.视频等精彩内容投射到电视上,让你感受大屏的刺激.而 ...

  3. bigdecimal的使用

    BigDecimal 由任意精度的整数非标度值 和 32 位的整数标度 (scale) 组成.如果为零或正数,则标度是小数点后的位数.如果为负数,则将该数的非标度值乘以 10 的负 scale 次幂. ...

  4. STL~Deque简介

    转自百度经验deque简介 deque是双向开口的连续性存储空间.虽说是连续性存储空间,但这种连续性只是表面上的,实际上它的内存是动态分配的,它在堆上分配了一块一块的动态储存区,每一块动态存储去本身是 ...

  5. 用vs2008和vs2005创建win32 console application

    http://blog.sina.com.cn/s/blog_4900be890100s735.html 对于经常使用vc6.0的人来说,创建一个win32 console application很简 ...

  6. windows 中使用hbase 异常:java.io.IOException: Could not locate executable null\bin\winutils.exe in the Hadoop binaries.

    平时一般是在windows环境下进行开发,在windows 环境下操作hbase可能会出现异常(java.io.IOException: Could not locate executable nul ...

  7. python爬虫beautifulsoup4系列2【转载】

    本篇转自博客:上海-悠悠 原文地址:http://www.cnblogs.com/yoyoketang/tag/beautifulsoup4/ 前言 本篇详细介绍beautifulsoup4的功能,从 ...

  8. CDH-5.7.0:基于Parcels方式离线安装配置

    http://shiyanjun.cn/archives/1728.html https://www.waitig.com/cdh%E5%AE%89%E8%A3%85.html

  9. [Math Review] Statistics Basic: Sampling Distribution

    Inferential Statistics Generalizing from a sample to a population that involves determining how far ...

  10. poj3264(Sparse-Table 算法模板)

    poj3264 题意 询问区间最大值最小值之差. 分析 dp_max[i][j] 表示以 i 为起点,长度为 \(2^j\) 的区间最大值. 利用递推预处理出区间最大值最小值. code #inclu ...