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 ...
随机推荐
- hexo+yilia页脚添加总访问量
个人博客:https://www.yuehan.online 现在博客:http://www.wangyurui.top 脚本方法使用不蒜子计数 安装脚本 要使用不蒜子(官网:http://busua ...
- 云原生应用开发12-Factors
英文地址:https://12factor.net/ 中文地址:https://12factor.net/zh_cn/ 文章内容 简介 如今,软件通常会作为一种服务来交付,它们被称为网络应用程序,或软 ...
- C#中字符串的内存分配与驻留池
完全引用http://www.cnblogs.com/instance/archive/2011/05/24/2056091.html 驻留池:是一张记录了所有在代码中使用字面量声明的字符串实例的引用 ...
- 每天一个Linux命令(56)yum命令
用于添加/删除/更新RPM包,自动解决包的依赖问题以及系统更新升级. (1)用法: 用法: yum [参数] [软件名] (2)功能: 功能: yum ...
- The Collections Module内建collections集合模块
https://www.bilibili.com/video/av17396749/?p=12 Python函数式编程中的迭代器,生成器详解 课程内容 1.iterators are objects ...
- HAproxy 介绍
HAproxy 介绍 (1)HAProxy 是一款提供高可用性.负载均衡以及基于TCP(第四层)和HTTP(第七层)应用的代理软件,支持虚拟主机,它是免费.快速并且可靠的一种解决方案. HAProxy ...
- Listening Carefully SP1403
Listening Carefully仔细聆听When people talk, listen completely. Most people never listen. ―Ernest Heming ...
- C/C++ 字符串操作函数 思维导图梳理
这些常用的字符串操作函数都是包在string.h头文件中. 分享此图,方便大家记忆 <(^-^)> 选中图片点击右键,在新标签页中打开图片会更清晰
- Cocos2d-x项目移植到WP8系列之四:文件操作
原文链接: http://www.cnblogs.com/zouzf/p/3972457.html 读写文件Cocos已经用fopen fwrite来做好了,这里说的主要是文件和文件夹的创建.删除.判 ...
- 让FireFox支持 window.event 全局事件对象
这里比原文稍加改进,让FF也支持 event.srcElement了, 省得每次写兼容代码挺麻烦的: //For firefox window.event if(typeof(window.event ...