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

思路:模拟乱搞一下,加个板子完事。

代码:

#include<cmath>
#include<set>
#include<map>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include <iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 550 * 64 + 10;
const int M = maxn * 30;
const ull seed = 131;
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
char s[maxn];
int bit[maxn * 8], now[maxn * 8];
map<int, int> change;
void init(){
change.clear();
for(int i = 0; i <= 25; i++)
change['A' + i] = i;
for(int i = 26; i <= 51; i++)
change['a' + i - 26] = i;
for(int i = 52; i <= 61; i++)
change['0' + i - 52] = i;
change['+'] = 62;
change['/'] = 63;
}
int sp(){
int len = strlen(s);
int num = 0;
for(int i = 0; i < len; i++){
if(s[i] == '='){
num -= 2;
continue;
}
int k = change[s[i]];
for(int j = 5; j >= 0; j--){
bit[num++] = ((k & (1 << j)) == 0? 0 : 1);
}
}
int cnt = 0;
for(int i = 0; i < num; i += 8){
now[cnt] = 0;
for(int j = i; j <= i + 7; j++){
now[cnt] = now[cnt] * 2 + bit[j];
}
cnt++;
}
return cnt;
} struct Aho{
struct state{
int next[260];
int fail, cnt;
}node[maxn];
int size;
queue<int> q; void init(){
size = 0;
newtrie();
while(!q.empty()) q.pop();
} int newtrie(){
memset(node[size].next, 0, sizeof(node[size].next));
node[size].cnt = node[size].fail = 0;
return size++;
} void insert(int s[], int len, int id){
int now = 0;
for(int i = 0; i < len; i++){
int c = s[i];
if(node[now].next[c] == 0){
node[now].next[c] = newtrie();
}
now = node[now].next[c];
}
node[now].cnt = id;
} void build(){
node[0].fail = -1;
q.push(0); while(!q.empty()){
int u = q.front();
q.pop();
for(int i = 0; i < 260; i++){
if(node[u].next[i]){
if(u == 0) node[node[u].next[i]].fail = 0;
else{
int v = node[u].fail;
while(v != -1){
if(node[v].next[i]){
node[node[u].next[i]].fail = node[v].next[i];
break;
}
v = node[v].fail;
}
if(v == -1) node[node[u].next[i]].fail = 0;
}
q.push(node[u].next[i]);
}
}
}
} set<int> res;
void get(int u){ //匹配规则
while(u){
if(node[u].cnt) res.insert(node[u].cnt);
u = node[u].fail;
}
} int match(int s[], int len){
res.clear();
int ret = 0, now = 0;
for(int i = 0; i < len; i++){
int c = s[i];
if(node[now].next[c]){
now = node[now].next[c];
}
else{
int p = node[now].fail;
while(p != -1 && node[p].next[c] == 0){
p = node[p].fail;
}
if(p == -1) now = 0;
else now = node[p].next[c];
}
get(now);
}
return res.size();
}
}ac;
int main(){
init();
int n;
while(~scanf("%d", &n)){
ac.init();
for(int i = 1; i <= n; i++){
scanf("%s", s);
int len = sp();
ac.insert(now, len, i);
}
ac.build();
int m;
scanf("%d", &m);
while(m--){
scanf("%s", s);
int len = sp();
printf("%d\n", ac.match(now, len));
}
printf("\n");
}
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自动机+解码】

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

  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. 415 Unsupported Media Type

    415 Unsupported Media Type - HTTP | MDN https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/415

  2. P6739 [BalticOI 2014 Day1] Three Friends 题解

    目录 写在前面 Solution 何为字符串哈希(可跳过): Code 写在前面 P6739 [BalticOI 2014 Day1] Three Friends 听说这题可以用比较暴力的做法过,比如 ...

  3. ASP.NET Core 5.0 MVC中的视图分类——布局视图、启动视图、具体视图、分部视图

    一.创建MVC应用程序 创建后的项目 二.(全局性)启动视图 _ViewStart.cshtml 顾名思义,就是在View开始执行之前执行,而且是每一个View, 它的预设内容是 @{ Layout ...

  4. Docker安装Mycat并实现mysql读写分离,分库分表

    Docker安装Mycat并实现mysql读写分离,分库分表 一.拉取mycat镜像 二.准备挂载的配置文件 2.1 创建文件夹并添加配置文件 2.1.1 server.xml 2.1.2 serve ...

  5. 内网渗透之信息收集-linux

    linux     系统信息         grep MemTotal /proc/meminfo #查看系统内存总量         cat /etc/issue #查看系统名称         ...

  6. liux 常用操作命令

    tail -f /home/jyapp/apache-tomcat-7.0.59/logs/catalina.out  //查看实施日志 //删除临时目录并且启动服务器 rm -rf /home/jy ...

  7. 学习一下 SpringCloud (四)-- 服务降级、熔断 Hystrix、Sentinel

    (1) 相关博文地址: 学习一下 SpringCloud (一)-- 从单体架构到微服务架构.代码拆分(maven 聚合): https://www.cnblogs.com/l-y-h/p/14105 ...

  8. MyBatis框架使用 —— 传递多个参数的方式

    引言 目前,MyBatis的使用越来越普遍,也有一些公司使用Hibernate.使用MyBatis需要我们自己书写SQL语句,面对各种复杂的场景,SQL传递多参是很普遍的.如何传递多参应对不同的场景也 ...

  9. Maven pom中的 scope 详解

    Maven的一个哲学是惯例优于配置(Convention Over Configuration), Maven默认的依赖配置项中,scope的默认值是compile,项目中经常傻傻的分不清,直接默认了 ...

  10. Think in Java 第三章操作符

    Think in Java 第三章操作符 赋值 对象赋值 ​ 我们真正操作的是对对象的引用.所以倘若"将一个对象赋值给另一个对象",实际上是将"引用"从一个地方 ...