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个编码后的主串,求原来主 ...
随机推荐
- 从.NET和Java之争谈IT这个行业[转]
一.有些事情难以回头 开篇我得表名自己的立场:.NET JAVA同时使用者,但更加偏爱.NET.原因很简单 1.NET语言更具开放性,从开源协议和规范可以看出; 2.语言更具优势严谨; 3.开发工具V ...
- iOS仿网易新闻栏目拖动重排添加删除效果
仿网易新闻栏目选择页面的基本效果,今天抽了点时间教大家如何实现UICollectionView拖动的效果! 其实实现起来并不复杂,这里只是基本的功能,没有实现细节上的修改,连UI都是丑丑的样子,随手画 ...
- StringBuffer与StringBuilder有什么区别
package String比较; /* * StringBuffer与StringBuilder有什么区别 * StringBuilder是JDK5增加的一个新类,功能几乎与StringBuffer ...
- SQLSERVER20008 完整备份和差异备份
--差异备份 DIFFERENTIAL ) ),)+'.bak' BACKUP DATABASE [testbackup] TO DISK=@name WITH DIFFERENTIAL, NOFOR ...
- Space Ant---poj1696(极角排序)
题目链接:http://poj.org/problem?id=1696 题意:给你n个点,然后我们用一条线把它们连起来,形成螺旋状: 首先找到左下方的一个点作为起点,然后以它为原点进行极角排序,找到极 ...
- win8.1蓝屏解决
按Windows 徽标键 +X键,点击“命令提示符(管理员)”,复制以下命令并运行: SFC /SCANNOW (此命令需要一段时间完成,不要关闭它,即使进度看上去停止不动.) reg add &qu ...
- webService 发送soap请求,并解析返回的soap报文
本例应用场景:要做一个webService测试功能,不局限于任何一种固定格式的webService,所以像axis,cxf等框架就不好用了.只有深入到webService的原理,通过发收soap报文, ...
- ios 实现版本更新检查
注:这里网络请求用的是第三方框架:SVHTTPRequest /* 第一步: 根据应用名称搜索应用,然后根据应用绑定的ID在结果中筛选出我们要找的应用,并取出应用的AppID */ - (void)g ...
- 转: 带你玩转Visual Studio——带你理解多字节编码与Unicode码
上一篇文章带你玩转Visual Studio——带你跳出坑爹的Runtime Library坑帮我们理解了Windows中的各种类型C/C++运行时库及它的来龙去脉,这是C++开发中特别容易误入歧途的 ...
- Ant执行Jmeter工程模版
<?xml version="1.0" encoding="GB2312"?><project name="ant-jmeter-t ...