ZOJ 3430 Detect the Virus(AC自动机)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3430
题意:给你n个编码后的模式串,和m个编码后的主串,求原来主串中含有模式串的个数
思路:首先要将模式串解码成未编码前来建立ac自动机,然后解码主串扫描统计即可。
code:
#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
using namespace std;
const int KIND = ;
const int MAXN = ; struct Trie
{
int next[MAXN][KIND], fail[MAXN], id[MAXN];
int root, L, num;
int create()
{
for (int i = ; i < KIND; ++i)
next[L][i] = -;
return L++;
}
void init()
{
L = ;
num = ;
memset(id, , sizeof(id));
root = create();
}
void insert(unsigned char str[], int len)
{
int now = root;
for (int i = ; i < len; ++i)
{
if (- == next[now][str[i]])
next[now][str[i]] = create();
now = next[now][str[i]];
}
id[now] = ++num;
}
void build()
{
queue<int>Q;
fail[root] = root;
for (int i = ; i < KIND; ++i)
{
if (- == next[root][i])
next[root][i] = root;
else
{
fail[next[root][i]] = root;
Q.push(next[root][i]);
}
}
while (!Q.empty())
{
int now = Q.front();
Q.pop();
for (int i = ; i < KIND; ++i)
{
if (- == next[now][i])
next[now][i] = next[fail[now]][i];
else
{
fail[next[now][i]] = next[fail[now]][i];
Q.push(next[now][i]);
}
}
}
}
int query(unsigned char str[], int len)
{
set<int>S;
int now = root;
for (int i = ; i < len; ++i)
{
now = next[now][str[i]];
int temp = now;
while (temp != root)
{
if (id[temp]) S.insert(id[temp]);
temp = fail[temp];
}
}
return (int)S.size();
}
}; Trie ac;
char buf[];
unsigned char temp[];
unsigned char str[]; unsigned char Tran(char ch)
{
if (ch >= 'A' && ch <= 'Z') return ch - 'A';
if (ch >= 'a' && ch <= 'z') return ch - 'a' + ;
if (ch >= '' && ch <= '') return ch - '' + ;
if (ch == '+') return ;
return ;
} int Decode(int len)
{
int k = ;
for (int i = ; i < len; i += )
{
str[k++] = (temp[i]<<)|(temp[i + ]>>);
if (i + < len) str[k++] = (temp[i + ]<<)|(temp[i + ]>>);
if (i + < len) str[k++] = (temp[i + ]<<)|(temp[i + ]);
}
return k;
} int main()
{
int n, m;
while (scanf("%d", &n) != EOF)
{
ac.init();
for (int i = ; i <n; ++i)
{
scanf("%s", buf);
int len = strlen(buf);
while ('=' == buf[len - ]) --len;
for (int j = ; j < len; ++j) temp[j] = Tran(buf[j]);
ac.insert(str, Decode(len));
}
ac.build();
scanf("%d", &m);
for (int i = ; i < m; ++i)
{
scanf("%s", buf);
int len = strlen(buf);
while ('=' == buf[len - ]) --len;
for (int j = ; j < len; ++j) temp[j] = Tran(buf[j]);
printf("%d\n", ac.query(str, Decode(len)));
}
printf("\n");
}
return ;
}
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自己主动机)
Detect the Virus Time Limit: 2 Seconds Memory Limit: 65536 KB One day, Nobita found that his co ...
- ZOJ 3430 Detect the Virus 【AC自动机+解码】
解码的那些事儿,不多说. 注意解码后的结果各种情况都有,用整数数组存储,char数组会超char类型的范围(这个事最蛋疼的啊)建立自动机的时候不能用0来判断结束. #include <cstdi ...
- 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}$)范围内,有多少数字的二进制表达式不包含模式串. ...
随机推荐
- 交通银行万事达Y-POWER信用卡 普卡
签账消费 免息尽享 失卡保护 风险全无 密码签名 任选 境外使用 本币还款 国内海外 环球支持 适合人群:年轻一族 发行状态:发行中 年费: 140元 币种: 人民币+美元 免年费政策:免首年 ...
- android:configChanges 屏幕横竖屏切换
出处:http://blog.csdn.net/djy1992/article/details/9378195 ---> android:screenOrientation="por ...
- 04737_C++程序设计_第8章_多态性和虚函数
例8.1 分析下面程序的输出结果. 例8.2 分别使用指针和引用的display函数. #include <iostream> using namespace std; const dou ...
- iOS禁用部分文件ARC
TARGETS的build Phases中的Compile Source里修改文件备注文件参数设定: 增加-fobjc-arc来使单个文件 支持ARC,或者添加-fno-objc-arc使单个文件不支 ...
- getHibernateTemplate().saveOrUpdate 不运行
在ssh中使用hibernateTemplate来保存对象的时候.出现一个问题,就是saveOrUpdate既不报错.也不在控制台打印插入语句,也不想数据库插入数据. 问题解决: 这个是事务的原因.检 ...
- css引入讲解及media
引用Css的几种方式: 一.@import <style type="text/css" media="screen"> @import url(& ...
- JavaScript之面向对象学习七(动态原型模式、寄生构造函数模式、稳妥构造函数模式创建自定义类型)
一.动态原型模式 在面向对象学习六中的随笔中,了解到组合构造函数模式和原型模式创建的自定义类型可能最完善的!但是人无完人,代码亦是如此! 有其他oo语言经验的开发人员在看到独立的构造函数和原型时,很可 ...
- 部署SharePoint2013解决方案
Add-SPSolutionInstall-SPSolution -Identity Grain2013.wsp -GACDeployment -CompatibilityLevel {14,15} ...
- Archlinux 安装配置指导 2015-05-24
因为用的Linode VPS的系统是Archlinux的,想在本地弄个系统做测试用,这样比较方便.然后发现自己在6年前做的一个Archlinux 安装配置Flash,好怀念的赶脚. 时过进迁,没想到A ...
- oracle归档日志
前几天因为导入大的东西,弄得很久都没动静,一看最后才发现是归档满了.但是很多的命令还是很是很不熟悉,所以看了下,百度了下.整理下这个. 1.查看归档日志大小及使用情况 select * from v$ ...