题目大意:给你一些病毒的特征码,然后再给一些文本,判断每个文本有多少种病毒,不过给的字符串都是加密处理过的,给的每个字符串都有对应一个64以内的一个数(题目里面那个表就是),然后可以把这个64以内的这个数化成6位的二进制数,然后把这些二进制数每8位再化成一个字符,这就是原来的字符,比如 QA== ->编号0 1->二进制010000 000000->每8位变成一个字符 01000000 0000(后面这4个0就是那两个==,可以舍去)-> 64('@')。
 
分析:因为是化成8位的二进制,2^8 = 256,不过因为有‘\0’这样的字符,所以无法使用char来搞,开一个int数组是个不错的选择,当然也得注意内存开销,申请内存的方式会MLE,实验得出结果开6W最好.....真是让人感觉很恶心,错了19次!!
 
代码如下:
===============================================================================================================================
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std; const int MAXN = ;
const int MAXM = ;///2^8
const int oo = 1e9+; const char cb64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int virus[MAXN], MumStr[MAXN];
char s[MAXN];
int password[MAXN*]; struct node
{
int Fail, next[MAXM];
int leaf, num; void InIt()
{
Fail = leaf = num = ;
memset(next, , sizeof(next));
} }trie[];
int top; int TurnStr(char s[], int p[])
{
int i, j, k=; for(i=; s[i] && s[i] != '='; i++)
{
s[i] = strchr(cb64, s[i]) - cb64; for(j=; j>=; j--)
{
password[k+j] = s[i] % ;
s[i] /= ;
} k += ;
} int len = ; for(i=j=; i<k; i++)
{
j = j* + password[i]; if((i+) % == )
{
p[len++] = j;
j = ;
}
} return len;
}
void Insert(int root, int N)
{
int p = root; for(int i=; i<N; i++)
{
int k = virus[i]; if(trie[p].next[k] == )
{
trie[p].next[k] = ++top;
trie[top].InIt();
}
p = trie[p].next[k];
} trie[p].leaf += ;
}
void GetFail(int root)
{
int p = root, temp;
queue<int> Q; trie[root].Fail = -; for(int i=; i<MAXM; i++)if(trie[p].next[i])
{
int k = trie[p].next[i]; trie[k].Fail = root;
Q.push(k);
} while(Q.size())
{
p = Q.front();
Q.pop(); for(int i=; i<MAXM; i++)if(trie[p].next[i])
{
int k = trie[p].next[i];
temp = trie[p].Fail; while(temp != -)
{
if(trie[temp].next[i])
{
trie[k].Fail = trie[temp].next[i];
break;
} temp = trie[temp].Fail;
} if(temp == -)
trie[k].Fail = root; Q.push(k);
}
}
}
int Query(int root, int N, int num)
{
int sum = ;
int p = root, temp; for(int i=; i<N; i++)
{
int k = MumStr[i]; while(!trie[p].next[k] && p!=root)
p = trie[p].Fail; if(!trie[p].next[k])continue; temp = p = trie[p].next[k]; while(temp != root && trie[temp].num != num)
{
if(trie[temp].leaf)
sum += ;
trie[temp].num = num;
temp = trie[temp].Fail;
}
} return sum;
} int main()
{
int N, M; while(scanf("%d", &N) != EOF)
{
int root = ; trie[].InIt();
top = ; for(int i=; i<=N; i++)
{
scanf("%s", s);
int len = TurnStr(s, virus); Insert(root, len);
} GetFail(root);
scanf("%d", &M); for(int i=; i<=M; i++)
{
scanf("%s", s);
int len = TurnStr(s, MumStr); printf("%d\n", Query(root, len, i));
}
printf("\n");
} return ;
}

 

Detect the Virus - ZOJ 3430(恶心的自动机)的更多相关文章

  1. Detect the Virus ZOJ - 3430 AC自动机

    One day, Nobita found that his computer is extremely slow. After several hours' work, he finally fou ...

  2. HDU - 2222,HDU - 2896,HDU - 3065,ZOJ - 3430 AC自动机求文本串和模式串信息(模板题)

    最近正在学AC自动机,按照惯例需要刷一套kuangbin的AC自动机专题巩固 在网上看过很多模板,感觉kuangbin大神的模板最为简洁,于是就选择了用kuangbin大神的模板. AC自动机其实就是 ...

  3. ZOJ - 3430 Detect the Virus —— AC自动机、解码

    题目链接:https://vjudge.net/problem/ZOJ-3430 Detect the Virus Time Limit: 2 Seconds      Memory Limit: 6 ...

  4. ZOJ 3430 Detect the Virus

    传送门: Detect the Virus                                                                                ...

  5. ZOJ 4114 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自己主动机)

    题目连接:zoj 3430 Detect the Virus 题目大意:给定一个编码完的串,将每个字符相应着表的数值转换成6位二进制.然后以8为一个数值,又一次形成字符 串,推断给定询问串是否含有字符 ...

  7. zoj 3430 Detect the Virus(AC自己主动机)

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

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

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

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

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

随机推荐

  1. 提供他人class文件

    1.考虑的问题,提供的文件是否依赖于其他jar包. 例如:解析html简历时,依赖于Jsoup包.

  2. hibernate4.3.8整合struts2过程中遇到的问题

    1.遇到的异常: Exception in thread "main" org.hibernate.service.spi.ServiceException: Unable to ...

  3. SQL三大范式

    第一范式:确保每列的原子性. 如果每列(或者每个属性)都是不可再分的最小数据单元(也称为最小的原子单元),则满足第一范式. 例如:顾客表(姓名.编号.地址.……)其中"地址"列还可 ...

  4. Spring 创建bean的模式

    在默认情况下,spring创建bean是单例模式 scope="singleton ",还有一种方式为多例模式[prototype]     scope          sing ...

  5. OPENGL 地形

    用OPNEGL弄了好久,终于有个地形的样子了! 看起来还是很糟糕....

  6. phpcms(3) V9 常用函数 及 代码整理(转)

    转自http://www.cnblogs.com/Braveliu/p/5103918.html 常用函数 及 常用代码 总结如下 <;?php //转换字符串或者数组的编码 str_chars ...

  7. JqGrid自定义toolbar

    1.设置toolbar参数为[true,"top"],其意思是toolbar显示在Grid顶部,且其id为t_+Grid的id.e.g.: Grid的id为myGrid,toolb ...

  8. 命令行,备份、导入数据库Oracle

    备份库:exp username/password@Database file="h:\datas.dmp" owner=username 导入语句 :imp username2/ ...

  9. IIS 服务器用OFFIC 2007 组件 WORD转PDF配置记录

    <system.web> <identity impersonate="true" userName="accountname" passwo ...

  10. 新一代的代码编辑神器Sublime Text 3(使用指南)

    首先附上官网下载链接:http://www.sublimetext.com/3 接下来是安装sublime最强大的插件功能:Package Control 一.简单的安装方法 使用Ctrl+`快捷键或 ...