题目大意:给你一些病毒的特征码,然后再给一些文本,判断每个文本有多少种病毒,不过给的字符串都是加密处理过的,给的每个字符串都有对应一个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. oracle模糊查询效率可这样提高

    1.使用两边加'%'号的查询,oracle是不通过索引的,所以查询效率很低. 例如:select count(*) from lui_user_base t where t.user_name lik ...

  2. CGContextRef一点用法

      quartz 是主要的描画接口,支持基于路径的描画.抗锯齿渲染.渐变填充模式.图像.颜色.坐标空间变换.以及PDF 文档的创建.显示.和分析.UIKit 为Quartz 的图像和颜色操作提供了Ob ...

  3. 极端气候频现 五款开发天气预报应用的API

    http://www.csdn.net/article/2014-02-07/2818322-weather-forecast-api-for-developing-apps

  4. NSCondition的用法,NSCondication实现线程同步,生产者消费问题实现(转载)

    NSCondition的用法 使用NSCondition,实现多线程的同步,即,可实现生产者消费者问题. 基本思路是,首先要创建公用的NSCondition实例.然后: 消费者取得锁,取产品,如果没有 ...

  5. C# 枚举

    一.在学习枚举之前,首先来听听枚举的优点. 1.枚举能够使代码更加清晰,它允许使用描述性的名称表示整数值. 2.枚举使代码更易于维护,有助于确保给变量指定合法的.期望的值. 3.枚举使代码更易输入. ...

  6. javascript事件详解1

    事件流讲解来袭,嘎嘎嘎嘎嘎 ---------------------------------------------------------------- 1.事件流:描述的是在页面中接受事件的顺序 ...

  7. underscorejs-countBy学习

    2.20 countBy 2.20.1 语法 _.countBy(list, iteratee, [context]) 2.20.2 说明 排序一个列表组成一个组,并且返回各组中的对象的数量的计数.类 ...

  8. js 判断时间,满足执行框架

    // 8点到早上19点关var curr = new Date();var time = curr.getHours(); if ( time >=0 && time <2 ...

  9. [Windows] php开发工具,zendstudio13使用方法补丁

    官网原版下载 http://downloads.zend.com/studio ... win32.win32.x86.exe 破解补丁: 链接:http://pan.baidu.com/s/1gdi ...

  10. 练习2 A - ASCII码排序

      Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Description 输入三 ...