hdu3695 ac自动机入门
Computer Virus on Planet Pandora
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 256000/128000 K (Java/Others) Total Submission(s): 1846 Accepted Submission(s): 510
For each test case:
The first line is a integer n( 0 < n <= 250) indicating the number of virus pattern strings.
Then n lines follows, each represents a virus pattern string. Every pattern string stands for a virus. It’s guaranteed that those n pattern strings are all different so there are
The last line of a test case is the program. The program may be described in a compressed format. A compressed program consists of capital letters and “compressors”.
[qx]
q is a number( 0 < q <= 5,000,000)and x is a capital letter. It means q consecutive letter xs in the original uncompressed program. For example, [6K] means ‘KKKKKK’
AB[2D]E[7K]G
It actually is ABDDEKKKKKKKG after decompressed to original format.
The length of the program is at least 1 and at most 5,100,000, no matter in the compressed format or after it is decompressed to original format.
In the second case in the sample input, the reverse of the program is ‘GHIFEDCCBA’, and ‘GHI’ is a substring of the reverse, so the program is infected
by virus ‘GHI’.
模板题:
#pragma comment(linker, "/STACK:16777216")
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <queue>
#include <string.h>
using namespace std; typedef struct abcd
{
abcd *next[];
int end;
abcd *fail;
}abcd;
int ans;
abcd *inti()
{
abcd *t;
t=(abcd *)malloc(sizeof(abcd));
for(int i=;i<;i++)
t->next[i]=NULL;
t->end=;
t->fail=NULL;
return t;
}
void insert(abcd *t,char z[])
{
if(*z=='\0')
{
t->end++;
return;
}
if(t->next[*z-'A']==NULL)
t->next[*z-'A']=inti();
insert(t->next[*z-'A'],z+);
}
void ac(abcd *t)
{
queue<abcd*>a;
while(!a.empty())a.pop();
for(int i=;i<;i++)
{
if(t->next[i]!=NULL)
{
t->next[i]->fail=t;
a.push(t->next[i]);
}
else t->next[i]=t;
}
abcd *r,*f;
while(!a.empty())
{
r=a.front();
a.pop();
for(int i=;i<;i++)
{
if(r->next[i])
{
a.push(r->next[i]);
f=r->fail;
while(!f->next[i])f=f->fail;
r->next[i]->fail=f->next[i];
}
}
}
}
void query(abcd *t,char x[])
{
abcd *f,*p=t;
while(*x)
{
while(!p->next[*x-'A'])p=p->fail;
p=p->next[*x-'A'];
f=p;
while(f!=t&&f->end!=-)
{
ans+=f->end;
f->end=-;
f=f->fail;
}
x++;
}
x--;
p=t;
while(*x)
{
while(!p->next[*x-'A'])p=p->fail;
p=p->next[*x-'A'];
f=p;
while(f!=t&&f->end!=-)
{
ans+=f->end;
f->end=-;
f=f->fail;
}
x--;
}
}
void del(abcd *t)
{
for(int i=;i<;i++)
{
if(!t->next[i])
del(t->next[i]);
}
free(t);
}
int main()
{
int tr,n,i;
//cout<<"YES"<<endl;
cin>>tr;
while(tr--)
{
abcd *t;
t=inti();
char z[];
cin>>n;
for(i=;i<n;i++)
{
cin>>z;
insert(t,z);
}
ac(t);
char x[];
int tt=,r;
char xx;
x[]='\0';
xx=getchar();
xx=getchar();
while(xx!='\n')
{
if(xx!='[')
x[tt++]=xx;
else
{
cin>>r;
xx=getchar();
for(int i=;i<r;i++)
x[tt++]=xx;
xx=getchar();
}
xx=getchar();
}
x[tt]='\0';
ans=;
query(t,x+);
cout<<ans<<endl;
del(t);
}
}
hdu3695 ac自动机入门的更多相关文章
- hdu2222 KeyWords Search AC自动机入门题
/** 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:题意:给定N(N <= 10000)个长度不大于50的模式串,再给定一个长度为L ...
- AC自动机入门
Aho-Corasick automaton,该算法在1975年产生于贝尔实验室,是著名的多模式匹配算法之一. KMP算法很好的解决了单模式匹配问题,如果有了字典树的基础,我们可以完美的结合二者解决多 ...
- HDU 2222 Keywords Search(AC自动机入门)
题意:给出若干个单词和一段文本,问有多少个单词出现在其中.如果两个单词是相同的,得算两个单词的贡献. 分析:直接就是AC自动机的模板了. 具体见代码: #include <stdio.h> ...
- hdu 2222 Keywords Search ac自动机入门
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...
- hdu2222之AC自动机入门
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdu2222 ac自动机入门
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdu 1277 AC自动机入门(指针版和数组版)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1277 推荐一篇博客(看思路就可以,实现用的是java): https://www.cnblogs.co ...
- HDU3695(AC自动机模板题)
题意:给你n个字符串,再给你一个大的字符串A,问你着n个字符串在正的A和反的A里出现多少个? 其实就是AC自动机模板题啊( ╯□╰ ) 正着query一次再反着query一次就好了 /* gyt Li ...
- hdu3065 病毒侵袭持续中 AC自动机入门题 N(N <= 1000)个长度不大于50的模式串(保证所有的模式串都不相同), 一个长度不大于2000000的待匹配串,求模式串在待匹配串中的出现次数。
/** 题目:hdu3065 病毒侵袭持续中 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3065 题意:N(N <= 1000)个长度不大于50的 ...
随机推荐
- python环境搭建和打包
安装: python是有两个版本的一个是2.x,一个是3.x,这两个版本是不兼容的所有请使用前看准版本.下面我们主要说3.5版本. Mac:https://www.python.org/ftp/pyt ...
- 读取xml文件中节点
/// <summary> /// /// </summary> /// <param name="xmlpath">节点路径</para ...
- 【搬运工】——初识Lua(转)
使用 Lua 编写可嵌入式脚本 Lua 提供了高级抽象,却又没失去与硬件的关联. 虽然编译性编程语言和脚本语言各自具有自己独特的优点,但是如果我们使用这两种类型的语言来编写大型的应用程序会是什么样子呢 ...
- 正则表达式 提取<A>标签
功能用途 主要实现了提取html代码中的a标签和url地址. 示例代码 Regex regex = new Regex("href\\s*=\\s*(?:\"(?<1> ...
- 插入排序与希尔排序Java实现
public class TestMain { public static void main(String[] args) { Integer[] a = new Integer[5000]; fo ...
- diff.js 列表对比算法 源码分析
diff.js列表对比算法 源码分析 npm上的代码可以查看 (https://www.npmjs.com/package/list-diff2) 源码如下: /** * * @param {Arra ...
- Web in Linux小笔记001
Linux灾难恢复: Root密码修复 Centos single Filesystem是硬盘文件根目录,无法再cd ..就像macitosh 硬盘图标 Pwd:显示绝对路径 MBR修复 模拟MBR被 ...
- 软工+C(2017第4期) Alpha/Beta换人
// 上一篇:超链接 // 下一篇:工具和结构化 注:在一次软件工程讨论课程进度设计的过程中,出现了这个关于 Alpha/Beta换人机制的讨论,这个机制在不同学校有不同的实施,本篇积累各方观点,持续 ...
- 团队作业4——第一次项目冲刺(Alpha版本)4.28
团队作业4--第一次项目冲刺(Alpha版本) Day seven: 会议照片 每日站立会议: 项目进展 今天是项目的Alpha敏捷冲刺的第七天,先大概整理下昨天已完成的任务以及今天计划完成的任务.今 ...
- 201521123096《Java程序设计》第八周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 2. 书面作业 本次作业题集集合 1.List中指定元素的删除(题目4-1) 1.1 实验总结 实验中使用了s ...