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个编码后的主串,求原来主 ...
随机推荐
- Python之编写函数
Python之编写函数 在Python中,定义一个函数要使用 def 语句,依次写出函数名.括号.括号中的参数和冒号:,然后,在缩进块中编写函数体,函数的返回值用 return 语句返回. 我们以自定 ...
- free-library-converts-2d-image-to-3d
http://www.i-programmer.info/news/105-artificial-intelligence/4917-free-library-converts-2d-image-to ...
- Sql Server 性能优化之包含列
导读:数据数优化查询一直是个比较热门的话题,小生在这方面也只能算是个入门生.今 天我们就讲下数据库包含列这个一项的作用及带来的优化效果 引用下MSDN里面的一段解释: 当查询中的所有列都作为键列或非键 ...
- Docker的私有仓库
server 192.168.1.107 registry ---push client 192.168.1.103 --pull [192.168.1.107 ...
- iOS,Xcod7/8,iOS使用修改点
1.Xcod7使用修改点 2.Xcode8使用修改点 Xcod7使用修改点 1.xcode7 新建的项目,Foundation下默认所有http请求都被改为https请求. HTTP+SSL/TLS+ ...
- 2Sigma OA prepare: Longest Chain
DP use HashMap: 根据string的长度sort,然后维护每个string的longest chain,default为1,如果删除某个char生成的string能提供更长的chain, ...
- 获取Python安装目录
>>> import sys>>> path=sys.executable>>> print (path)C:\Users\jumz-G\AppD ...
- 提取SD卡中的图片
读取SD卡的权限 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> ...
- go log repl
package main import ( "fmt" "os/exec" "regexp" "strconv" &qu ...
- 机器学习实战-边学边读python代码(3)
程序清单2-3 归一化特征值: def autoNorm(dataSet): /* >>> barray([[ 1., 2., 3.], [ 2., 3., 4.], [ 10., ...