ZOJ - 3430 ac自动机
这题主要就是解码过程很恶心,不能用char存,一共wa了20发
题意:先给n串加密后的字符,然后m串加密后的字符,解码之后求n对应每个m的匹配数,很显然的ac自动机
加密过程是先用对应ascii表的标号来代替字符,然后把这些数字转换成8位的二进制,全部连起来,然后每6位算一个数,用二进制算成整数,最后用这些整数来映射给定的表
题解:反解密就好了,要注意细节,很容易Segmentation Fault,最后算出来的字符用int存,然后跑ac自动机就行了
#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define C 0.5772156649
#define pi acos(-1.0)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1 using namespace std; const double g=10.0,eps=1e-;
const int N=*+,maxn=+,inf=0x3f3f3f; inline void debug(){cout<<"fuck"<<endl;} char s[];
int de[],sz;
int digit[*];
struct Trie{
int tot,root;
int Next[N][],fail[N],End[N];
int newnode()
{
for(int i=;i<;i++)
Next[tot][i]=-;
End[tot]=;
return tot++;
}
void init()
{
tot=;
root=newnode();
}
void insertstring(int id)
{
int now=root;
for(int i=;i<sz;i++)
{
if(Next[now][de[i]]==-)
Next[now][de[i]]=newnode();
now=Next[now][de[i]];
}
End[now]=id;
}
void build()
{
queue<int>q;
fail[root]=root;
for(int i=;i<;i++)
{
if(Next[root][i]==-)Next[root][i]=root;
else
{
fail[Next[root][i]]=root;
q.push(Next[root][i]);
}
}
while(!q.empty())
{
int now=q.front();
q.pop();
for(int i=;i<;i++)
{
if(Next[now][i]==-)Next[now][i]=Next[fail[now]][i];
else
{
fail[Next[now][i]]=Next[fail[now]][i];
q.push(Next[now][i]);
}
}
}
}
bool vis[];
int query(int n)
{
int now=root;
memset(vis,,sizeof vis);
for(int i=;i<sz;i++)
{
now=Next[now][de[i]];
int te=now;
while(te!=root)
{
if(End[te])vis[End[te]]=;
te=fail[te];
}
}
int res=;
for(int i=;i<=n;i++)
if(vis[i])
res++;
return res;
}
};
int getnew(char x)
{
if(x>='A'&&x<='Z')return x-'A';
if(x>='a'&&x<='z')return x-'a'+;
if(x>=''&&x<='')return x-''+;
if(x=='+')return ;
else return ;
}
void change(char s[])
{
sz=;
int len=strlen(s);
while(s[len-]=='=')len--;
vector<int>v;
for(int i=;i<len;i++)v.pb(getnew(s[i]));
memset(digit,,sizeof digit);
for(int i=;i<v.size();i++)
{
for(int j=*(i+)-;j>=*i;j--)
{
if(v[i]&)digit[j]=;
v[i]/=;
}
}
sz=v.size()*/;
for(int i=;i<sz;i++)
{
de[i]=;
for(int j=*i;j<*(i+);j++)
de[i]=(de[i]<<)+digit[j];
// cout<<de[i]<<" ";
}
// cout<<endl;
}
Trie ac;
int main()
{
ios::sync_with_stdio(false);
cin.tie();
int n;
while(~scanf("%d",&n))
{
ac.init();
for(int i=;i<=n;i++)
{
scanf("%s",s);
change(s);
ac.insertstring(i);
}
ac.build();
int k;
scanf("%d",&k);
while(k--)
{
scanf("%s",s);
change(s);
printf("%d\n",ac.query(n));
}
puts("");
}
return ;
}
/******************** ********************/
ZOJ - 3430 ac自动机的更多相关文章
- HDU - 2222,HDU - 2896,HDU - 3065,ZOJ - 3430 AC自动机求文本串和模式串信息(模板题)
最近正在学AC自动机,按照惯例需要刷一套kuangbin的AC自动机专题巩固 在网上看过很多模板,感觉kuangbin大神的模板最为简洁,于是就选择了用kuangbin大神的模板. AC自动机其实就是 ...
- Detect the Virus ZOJ - 3430 AC自动机
One day, Nobita found that his computer is extremely slow. After several hours' work, he finally fou ...
- ZOJ 3494 (AC自动机+高精度数位DP)
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3494 题目大意:给定一些被禁止的BCD码.问指定范围内不含有 ...
- BCD Code ZOJ - 3494 AC自动机+数位DP
题意: 问A到B之间的所有整数,转换成BCD Code后, 有多少个不包含属于给定病毒串集合的子串,A,B <=10^200,病毒串总长度<= 2000. BCD码这个在数字电路课上讲了, ...
- Searching the String ZOJ - 3228 AC自动机查询升级版
题意:先给你一个不超过1000000长度的大串s:接下来输入一个n代表接下来输入的小串个数,小串长度不超过6. 小串分两种类型0和1类型. 0类型表示小串在大串中的最大匹配个数就是常规的AC自动机的做 ...
- ZOJ 3430 Detect the Virus(AC自动机)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3430 题意:给你n个编码后的模式串,和m个编码后的主串,求原来主 ...
- ZOJ - 3430 Detect the Virus —— AC自动机、解码
题目链接:https://vjudge.net/problem/ZOJ-3430 Detect the Virus Time Limit: 2 Seconds Memory Limit: 6 ...
- ZOJ 3494 BCD Code(AC自动机+数位DP)
BCD Code Time Limit: 5 Seconds Memory Limit: 65536 KB Binary-coded decimal (BCD) is an encoding ...
- ZOJ 4114 Detect the Virus(AC自动机)
Detect the Virus Time Limit: 2 Seconds Memory Limit: 65536 KB One day, Nobita found that his co ...
随机推荐
- python 获取当前运行的 class 和 方法的名字
# coding=utf-8 import sys class Hello(): def hello(self): print('the name of method is ## {} ##'.for ...
- C++程序设计练习(一)
// 1. 在屏幕上输出内容 #include<iostream> using namespace std; int main(){ int i= 1; cout<<" ...
- If you ever have a broken heart
If you ever have a broken heart Pablo Neruda Tonight i can write the saddest lines Write ,for exampl ...
- 查看连接MYSQL数据库的IP信息
要统计数据库的连接数,我们通常情况下是统计总数,细分到每个ip地址: 方法一: ) as ip , count(*) from information_schema.processlist group ...
- python中的标识符长度能有多长
在python中,标识符可以还是任意长度.此外,我们在命名标识符时还必须遵守以下规则 1 只能以下划线或者A-Z/a-z中字母开头 2 其余部分可以使用A-Z/a-z/0-9 3 区分大小写 4 关键 ...
- document.documentElement和document.body区别以及获取浏览器的宽高
原文:http://www.jb51.net/article/41410.htm 1.区别: body是DOM对象里的body子节点,即 <body> 标签: documentElemen ...
- Cookie用法简介
java操作Cookie---javax.servlet.http.Cookie 1.增加一个Cookie Cookie cookie = new Cookie("username" ...
- libsvm+eclipse(java)的配置以及开发需要设置的内容
主要参考博客: 1.eclipse + libsvm-3.12 用SVM实现简单线性分类 cnBlog中的主要介绍如何导入jar包的问题. 2.LIBSVM入门解读 CSDN,主要是对LIB ...
- 网络:W5500抓包TCP segment of a reassembled PDU
1.问题描述 W5500 http测试,用wireshark抓包,发现出现很多TCP segment of a reassembled PD. 2. 问题分析 TCP segment of a rea ...
- VoLTE的前世今生...说清楚VoIP、VoLTE、CSFB、VoWiFi、SIP、IMS那些事...
转:https://mp.weixin.qq.com/s?__biz=MzA3MTA3OTIwMw==&mid=401344844&idx=1&sn=497b351f524af ...