ZOJ 3430 Detect the Virus(AC自动机)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3430
题意:给你n个编码后的模式串,和m个编码后的主串,求原来主串中含有模式串的个数
思路:首先要将模式串解码成未编码前来建立ac自动机,然后解码主串扫描统计即可。
code:
#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
using namespace std;
const int KIND = ;
const int MAXN = ; struct Trie
{
int next[MAXN][KIND], fail[MAXN], id[MAXN];
int root, L, num;
int create()
{
for (int i = ; i < KIND; ++i)
next[L][i] = -;
return L++;
}
void init()
{
L = ;
num = ;
memset(id, , sizeof(id));
root = create();
}
void insert(unsigned char str[], int len)
{
int now = root;
for (int i = ; i < len; ++i)
{
if (- == next[now][str[i]])
next[now][str[i]] = create();
now = next[now][str[i]];
}
id[now] = ++num;
}
void build()
{
queue<int>Q;
fail[root] = root;
for (int i = ; i < KIND; ++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 < KIND; ++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]);
}
}
}
}
int query(unsigned char str[], int len)
{
set<int>S;
int now = root;
for (int i = ; i < len; ++i)
{
now = next[now][str[i]];
int temp = now;
while (temp != root)
{
if (id[temp]) S.insert(id[temp]);
temp = fail[temp];
}
}
return (int)S.size();
}
}; Trie ac;
char buf[];
unsigned char temp[];
unsigned char str[]; unsigned char Tran(char ch)
{
if (ch >= 'A' && ch <= 'Z') return ch - 'A';
if (ch >= 'a' && ch <= 'z') return ch - 'a' + ;
if (ch >= '' && ch <= '') return ch - '' + ;
if (ch == '+') return ;
return ;
} int Decode(int len)
{
int k = ;
for (int i = ; i < len; i += )
{
str[k++] = (temp[i]<<)|(temp[i + ]>>);
if (i + < len) str[k++] = (temp[i + ]<<)|(temp[i + ]>>);
if (i + < len) str[k++] = (temp[i + ]<<)|(temp[i + ]);
}
return k;
} int main()
{
int n, m;
while (scanf("%d", &n) != EOF)
{
ac.init();
for (int i = ; i <n; ++i)
{
scanf("%s", buf);
int len = strlen(buf);
while ('=' == buf[len - ]) --len;
for (int j = ; j < len; ++j) temp[j] = Tran(buf[j]);
ac.insert(str, Decode(len));
}
ac.build();
scanf("%d", &m);
for (int i = ; i < m; ++i)
{
scanf("%s", buf);
int len = strlen(buf);
while ('=' == buf[len - ]) --len;
for (int j = ; j < len; ++j) temp[j] = Tran(buf[j]);
printf("%d\n", ac.query(str, Decode(len)));
}
printf("\n");
}
return ;
}
ZOJ 3430 Detect 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 ...
- zoj 3430 Detect the Virus(AC自己主动机)
题目连接:zoj 3430 Detect the Virus 题目大意:给定一个编码完的串,将每个字符相应着表的数值转换成6位二进制.然后以8为一个数值,又一次形成字符 串,推断给定询问串是否含有字符 ...
- ZOJ 3430 Detect the Virus
传送门: Detect the Virus ...
- zoj 3430 Detect the Virus(AC自己主动机)
Detect the Virus Time Limit: 2 Seconds Memory Limit: 65536 KB One day, Nobita found that his co ...
- ZOJ 3430 Detect the Virus 【AC自动机+解码】
解码的那些事儿,不多说. 注意解码后的结果各种情况都有,用整数数组存储,char数组会超char类型的范围(这个事最蛋疼的啊)建立自动机的时候不能用0来判断结束. #include <cstdi ...
- ZOJ 3430 Detect the Virus(AC自动机 + 模拟)题解
题意:问你主串有几种模式串.但是所有串都是加密的,先解码.解码过程为:先把串按照他给的映射表变成6位数二进制数,然后首尾衔接变成二进制长串,再8位8位取变成新的数,不够的补0.因为最多可能到255,所 ...
- ZOJ 3228 Searching the String(AC自动机)
Searching the String Time Limit: 7 Seconds Memory Limit: 129872 KB Little jay really hates to d ...
- ZOJ 4114 Detect the Virus(AC自动机)
Detect the Virus Time Limit: 2 Seconds Memory Limit: 65536 KB One day, Nobita found that his co ...
- ZOJ 3494 BCD Code(AC自动机 + 数位DP)题解
题意:每位十进制数都能转化为4位二进制数,比如9是1001,127是 000100100111,现在问你,在L到R(R <= $10^{200}$)范围内,有多少数字的二进制表达式不包含模式串. ...
随机推荐
- MyBatis 最常见错误,启动时控制台无限输出日志
你是否遇到过下面的情况,控制台无限的输出下面的日志: Logging initialized using ‘class org.apache.ibatis.logging.log4j.Log4jImp ...
- lodash中_.set的用法
_.set(object, path, value) # Ⓢ Ⓣ Ⓝ 设置对象的路径上的属性值.如果路径不存在,则创建它. 参数 1.object (Object): 待扩大的对象. 2.path ( ...
- 剑指offer 25 二叉树中和为某一值的路径
非递归方法: class Solution { public: vector<vector<int>> FindPath(TreeNode* root,int expectNu ...
- 解决cookie无法删除的问题
今天遇到一个cookie无法删除的问题,退出操作时cookie无法删除,必须在首页先进行退出操作,后来发现一个网友的博客,介绍了无法删除Cookie的原因,原来是我关于cookie的基础知识没搞清楚. ...
- 比较两个data日期之间的天数相差
先将字符串转化为Date类型 转化方式参看:http://blog.csdn.net/caoyinghui1986/archive/2008/04/18/2303570.aspx 然后在调用Date. ...
- 全球最快的JS模板引擎
废话不多说,先上测试: 亲测请访问:[在线测试地址]单次结果不一定准确,请多测几次. tppl 的编译渲染速度是著名的 jQuery 作者 John Resig 开发的 tmpl 的 43 倍!与第二 ...
- Win10下Genymotion无法正常使用的解决方法
原Win7下安装配置的genymotion正常使用,Eclipse的Genymotion插件也可以正常运行.系统升级后,忽然就不work了. 折腾了一天试了各种方式,网上的例子也五花八门.最后还是找到 ...
- Httpservlet cannot be resolved to a type
这个问题与上个问题可以说是“错的类似”.解决方案:就是在Tomcat的lib目录下加入servlet-api.jar 即可.
- .wsdl文件生成.cs文件
1.打开VS文件命令行工具(一般在安装文件的Common7\Tools\Shortcuts下面例如[E:\VS2013安装程序\Common7\Tools\Shortcuts\VS2013 x64 本 ...
- Unity5UGUI 官方教程学习笔记(二)Rect Transform
Rect Transform Posx Posy Posz : ui相对于父级的位置 Anchors :锚点 定义了与父体之间的位置关系 一个锚点由四个锚组成 四个锚分别代表了 ...