Detect the Virus - ZOJ 3430(恶心的自动机)
#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(恶心的自动机)的更多相关文章
- Detect the Virus ZOJ - 3430 AC自动机
One day, Nobita found that his computer is extremely slow. After several hours' work, he finally fou ...
- HDU - 2222,HDU - 2896,HDU - 3065,ZOJ - 3430 AC自动机求文本串和模式串信息(模板题)
最近正在学AC自动机,按照惯例需要刷一套kuangbin的AC自动机专题巩固 在网上看过很多模板,感觉kuangbin大神的模板最为简洁,于是就选择了用kuangbin大神的模板. 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
传送门: Detect the Virus ...
- 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 3430 Detect the Virus(AC自己主动机)
题目连接:zoj 3430 Detect the Virus 题目大意:给定一个编码完的串,将每个字符相应着表的数值转换成6位二进制.然后以8为一个数值,又一次形成字符 串,推断给定询问串是否含有字符 ...
- 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自动机)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3430 题意:给你n个编码后的模式串,和m个编码后的主串,求原来主 ...
- ZOJ 3430 Detect the Virus 【AC自动机+解码】
解码的那些事儿,不多说. 注意解码后的结果各种情况都有,用整数数组存储,char数组会超char类型的范围(这个事最蛋疼的啊)建立自动机的时候不能用0来判断结束. #include <cstdi ...
随机推荐
- oracle模糊查询效率可这样提高
1.使用两边加'%'号的查询,oracle是不通过索引的,所以查询效率很低. 例如:select count(*) from lui_user_base t where t.user_name lik ...
- CGContextRef一点用法
quartz 是主要的描画接口,支持基于路径的描画.抗锯齿渲染.渐变填充模式.图像.颜色.坐标空间变换.以及PDF 文档的创建.显示.和分析.UIKit 为Quartz 的图像和颜色操作提供了Ob ...
- 极端气候频现 五款开发天气预报应用的API
http://www.csdn.net/article/2014-02-07/2818322-weather-forecast-api-for-developing-apps
- NSCondition的用法,NSCondication实现线程同步,生产者消费问题实现(转载)
NSCondition的用法 使用NSCondition,实现多线程的同步,即,可实现生产者消费者问题. 基本思路是,首先要创建公用的NSCondition实例.然后: 消费者取得锁,取产品,如果没有 ...
- C# 枚举
一.在学习枚举之前,首先来听听枚举的优点. 1.枚举能够使代码更加清晰,它允许使用描述性的名称表示整数值. 2.枚举使代码更易于维护,有助于确保给变量指定合法的.期望的值. 3.枚举使代码更易输入. ...
- javascript事件详解1
事件流讲解来袭,嘎嘎嘎嘎嘎 ---------------------------------------------------------------- 1.事件流:描述的是在页面中接受事件的顺序 ...
- underscorejs-countBy学习
2.20 countBy 2.20.1 语法 _.countBy(list, iteratee, [context]) 2.20.2 说明 排序一个列表组成一个组,并且返回各组中的对象的数量的计数.类 ...
- js 判断时间,满足执行框架
// 8点到早上19点关var curr = new Date();var time = curr.getHours(); if ( time >=0 && time <2 ...
- [Windows] php开发工具,zendstudio13使用方法补丁
官网原版下载 http://downloads.zend.com/studio ... win32.win32.x86.exe 破解补丁: 链接:http://pan.baidu.com/s/1gdi ...
- 练习2 A - ASCII码排序
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 输入三 ...