zoj3430Detect the Virus(ac自动机)
解码之后是跟普通的自动机求解一下的,只不过解码比较恶心,512=》N》=0 ,所以不能用字符串来存,需要转换成整数来做。
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
using namespace std;
#define N 53769
#define NN 15010
#define LL long long
#define INF 0xfffffff
const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>;
const int child_num = ;
char s[NN],vir[NN];
int di[NN*],od[NN],vv[NN];
bool f[N];
class ACAutomation
{
private:
int ch[N][child_num];
int val[N];
int fail[N];
int Q[N];
int id[];
int sz;
// int dp[2][N][1<<10];
public:
void init()
{
fail[] = ;
for(int i = ; i < child_num; i++)
id[i] = i;
}
void reset()//初始化
{
memset(ch[],,sizeof(ch[]));
memset(val,,sizeof(val));
sz = ;
}
void insert(int *a,int key,int k)//建立trie树
{
int p = ;
for( int i = ; i < k ; i++)
{
int c = id[a[i]];
if(ch[p][c]==)
{
memset(ch[sz],,sizeof(ch[sz]));
ch[p][c] = sz++;
}
p = ch[p][c];
}
val[p] += key;
}
void construct()//构建fail指针
{
int head = ,tail = ,i;
for(i = ;i < child_num ; i++)
{
if(ch[][i])
{
fail[ch[][i]] = ;
Q[tail++] = ch[][i];
}
}
while(head!=tail)
{
int u = Q[head++];
for(i = ;i < child_num ;i ++)
{
if(ch[u][i]!=)
{
Q[tail++] = ch[u][i];
fail[ch[u][i]] = ch[fail[u]][i];
}
else
ch[u][i] = ch[fail[u]][i];
}
}
}
int work(int *s,int k)
{
int p = ,ans = ;
memset(f,,sizeof(f));
for(int i = ; i < k ; i++)
{
int d = id[s[i]];
p = ch[p][d];
int tmp = p;
while(tmp!=&&!f[tmp])
{
ans+=val[tmp];
f[tmp] = ;
tmp = fail[tmp];
}
}
return ans;
}
}ac;
int change(char *a)
{
int i,k = strlen(a),j;
int g = ,gg,num = ;
for(i = ;i < k ; i++)
{
if(a[i]=='=')
{
num++;
continue;
}
int x = od[a[i]];gg=;
for(j=;j>=;j--)
{
di[g++]=((x&(<<j))>);
}
}
if(num==)
g-=;
else if(num==)
g-=;
gg = ;
int y = ;
for(j = ;j < g ; j++)
{
y+=di[j]*(<<(-j%));
if((j+)%==)
{
vv[gg++] = y;
y = ;
}
}
return g/;
}
int main()
{
int n,i,m;
for(i = 'A' ; i <= 'Z' ; i++)
od[i] = i-'A';
for(i = 'a' ; i <= 'z' ; i++)
od[i] = +i-'a';
for(i = ''; i <= '' ; i++)
od[i] = +i-'';
od['+'] = ,od['/'] = ;
ac.init();
while(scanf("%d",&n)!=EOF)
{
ac.reset();
for(i = ; i <= n; i++)
{
scanf("%s",vir);
int len = change(vir);
ac.insert(vv,,len);
}
ac.construct();
scanf("%d",&m);
for(i = ; i <= m ;i++)
{
scanf("%s",s);
int len = change(s);
printf("%d\n",ac.work(vv,len));
}
puts("");
}
return ;
}
zoj3430Detect 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 ...
- hdu 3695:Computer Virus on Planet Pandora(AC自动机,入门题)
Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 256000/1280 ...
- HDU 3695 / POJ 3987 Computer Virus on Planet Pandora(AC自动机)(2010 Asia Fuzhou Regional Contest)
Description Aliens on planet Pandora also write computer programs like us. Their programs only consi ...
- ZOJ 4114 Detect the Virus(AC自动机)
Detect the Virus Time Limit: 2 Seconds Memory Limit: 65536 KB One day, Nobita found that his co ...
- hdu 3695 10 福州 现场 F - Computer Virus on Planet Pandora 暴力 ac自动机 难度:1
F - Computer Virus on Planet Pandora Time Limit:2000MS Memory Limit:128000KB 64bit IO Format ...
- Detect the Virus ZOJ - 3430 AC自动机
One day, Nobita found that his computer is extremely slow. After several hours' work, he finally fou ...
- AC自动机 - 多模式串的匹配 --- HDU 3695 Computer Virus on Planet Pandora
Problem's Link Mean: 有n个模式串和一篇文章,统计有多少模式串在文章中出现(正反统计两次). analyse: 好久没写AC自动机了,回顾一下AC自动机的知识. 本题在构造文章的时 ...
- [AC自动机]HDOJ3695 Computer Virus on Planet Pandora
题意:给t.n,t个案例,n个字符串 下面给n+1个字符串,n个互不相同的小串,最后一个是模式串 模式串会出现[qx]的形式,q为数字,x为一个字母 问n个小串在模式串中出现的个数,正着出现.反着出现 ...
- ZOJ 3430 Detect the Virus(AC自动机)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3430 题意:给你n个编码后的模式串,和m个编码后的主串,求原来主 ...
随机推荐
- [troubleshoot][daily][archlinux][pacman] pacman 与 pip 包文件冲突
今天滚系统,再次遭遇包冲突: (/) checking % error: failed to commit transaction (conflicting files) python2-reques ...
- Tomcat热部署方法(3种)【转】
热部署是指在你修改项目BUG的时候对JSP或JAVA类进行了修改在不重启WEB服务器前提下能让修改生效.但是对配置文件的修改除外! 1.直接把项目web文件夹放在webapps里. 2.在tomcat ...
- 【五子棋AI循序渐进】——开局库
首先,对前面几篇当中未修复的BUG致歉,在使用代码时请万分小心…………尤其是前面关于VCF\VCT的一些代码和思考,有一些错误.虽然现在基本都修正了,但是我的程序还没有经过非常大量的对局,在这之前,不 ...
- angularjs backbone 集成requirejs 模块化
首先认识requirejs requirejs是个包加载器,核心功能是模块化管理,可以实现按需加载. 重点是明白 模块化不是按需加载. 模块化的意义: 是通过代码逻辑表明模块之间的依赖关系和执行顺序, ...
- MySQL中文全文检索
一.概述 MySQL全文检索是利用查询关键字和查询列内容之间的相关度进行检索,可以利用全文索引来提高匹配的速度. 二.语法 MATCH (col1,col2,...) AGAINS ...
- LeetCode Palindrome Permutation II
原题链接在这里:https://leetcode.com/problems/palindrome-permutation-ii/ 题目: Given a string s, return all th ...
- mongoDB(3) mapReduce
mapReduce是大数据的核心内容,但实际操作中别用这个,所谓的mapReduce分两步 1.map:将数据分别取出,Map函数调用emit(key,value)遍历集合中所有的记录,将key与va ...
- 《android 导入第三方源码jar包遇到的坑》
最近想做个app,里面需要有一个二维码扫描的功能,然后谷歌之后发现Zxing这个用的人好多,就看看怎么用: 然后就在github上拉下他们的源码,导入eclipse,然后编译之后导出为jar文件[用的 ...
- selected对话框全选
selected对话框全选 <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...
- 关于HTML中标签<a>使用js的注意事项
以下两点都不可取: 1.<a href="#" onClick="popUp('http://www.baidu.com');return false;" ...