HDU3695 - Computer Virus on Planet Pandora(AC自动机)
题目大意
给定一个文本串T,然后给定n个模式串,问有多少个模式串在文本串中出现,正反都可以
题解
建立好自动机后。把文本串T正反各匹配一次,刚开始一直TLE。。。后面找到原因是重复的子串很多以及有模式串是另外一个模式串的子串这种情况也很多~~~,所以我们用数组标记一下就好了~~~改了交上去之后是WA,最后发现时visit数组初始化错地方了。。。。
代码:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <string>
#include <queue>
using namespace std;
const int maxnode=500005;
const int sigma_size=26;
const int MAXN=5100005;
char T[MAXN],s[1005];
bool visit[maxnode];
int ans;
struct Triegragh
{
int ch[maxnode][sigma_size];
int fail[maxnode];
int val[maxnode];
int last[maxnode];
int sz;
void init()
{
memset(ch[0],0,sizeof(ch[0]));
memset(visit,false,sizeof(visit));
sz=1;
}
int idx(char c)
{
return c-'A';
}
void insert(char *s)
{
int u=0,n=strlen(s);
for(int i=0; i<n; i++)
{
int c=idx(s[i]);
if(!ch[u][c])
{
memset(ch[sz],0,sizeof(ch[sz]));
val[sz]=0;
ch[u][c]=sz++;
}
u=ch[u][c];
}
val[u]++;
}
void getfail()
{
queue<int>q;
fail[0]=0;
for(int c=0; c<sigma_size; c++)
{
int u=ch[0][c];
if(u)
{
fail[u]=0;
q.push(u);
last[u]=0;
}
}
while(!q.empty())
{
int r=q.front();
q.pop();
for(int c=0; c<sigma_size; c++)
{
int u=ch[r][c];
if(!u)
{
ch[r][c]=ch[fail[r]][c];
continue;
}
fail[u]=ch[fail[r]][c];
q.push(u);
last[u]=val[fail[u]]?fail[u]:last[fail[u]];
}
}
}
void count(int j)
{
if(j)
{
if(!visit[j]) ans++;
else return;
visit[j]=true;
count(last[j]);
}
}
void find(char *T)
{
int n=strlen(T);
int j=0;
for(int i=0; i<n; i++)
{
int c=idx(T[i]);
j=ch[j][c];
if(val[j])
count(j);
else if(last[j]) count(last[j]);
}
}
};
Triegragh ac;
int main()
{
int kase;
scanf("%d",&kase);
while(kase--)
{
int n;
scanf("%d",&n);
ac.init();
for(int i=1; i<=n; i++)
{
scanf("%s",s);
ac.insert(s);
}
ac.getfail();
getchar();
char c;
int j=0;
while(c=getchar(),c!='\n')
{
if(c=='[')
{
int m;
scanf("%d",&m);
c=getchar();
for(int i=0; i<m; i++)
T[j++]=c;
getchar();
}
else T[j++]=c;
}
T[j]='\0';
ans=0;
ac.find(T);
reverse(T,T+strlen(T));
ac.find(T);
printf("%d\n",ans);
}
return 0;
}
HDU3695 - Computer Virus on Planet Pandora(AC自动机)的更多相关文章
- hdu ----3695 Computer Virus on Planet Pandora (ac自动机)
Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 256000/1280 ...
- POJ 3987 Computer Virus on Planet Pandora (AC自动机优化)
题意 问一个字符串中包含多少种模式串,该字符串的反向串包含也算. 思路 解析一下字符串,简单. 建自动机的时候,通过fail指针建立trie图.这样跑图的时候不再跳fail指针,相当于就是放弃了fai ...
- HDU-3695 Computer Virus on Planet Pandora
HDU-3695 Computer Virus on Planet Pandora 题意:电脑中病毒了, 现在n钟病毒指令, 然后有一个电脑指令, 看一下这个电脑指令中了几个病毒, 如果电脑种了某一个 ...
- hdu 3695 Computer Virus on Planet Pandora(AC自己主动机)
题目连接:hdu 3695 Computer Virus on Planet Pandora 题目大意:给定一些病毒串,要求推断说给定串中包括几个病毒串,包括反转. 解题思路:将给定的字符串展开,然后 ...
- 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 10 福州 现场 F - Computer Virus on Planet Pandora 暴力 ac自动机 难度:1
F - Computer Virus on Planet Pandora Time Limit:2000MS Memory Limit:128000KB 64bit IO Format ...
- 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
Computer Virus on Planet Pandora Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1353 ...
- 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 ...
随机推荐
- Authentication for the REST APIs
HTTP基本认证原理 在HTTP协议进行通信的过程中,HTTP协议定义了基本认证过程以允许HTTP服务器对WEB浏览器进行用户身份认证的方法,当一个客户端向HTTP服务器进行数据请求时,如果客户端未被 ...
- spring beans源码解读
spring beans下面有如下源文件包: org.springframework.beans, 包含了操作java bean的接口和类.org.springframework.beans.anno ...
- JavaScript: top对象
一般的JS书里都会在讲框架集的时候讲top,这会让人误解,认为top对象只是代表框架集,其实top的含义应该是说浏览器直接包含的那一个页面对象,也就是说如果你有一个页面被其他页面以iframe的方式包 ...
- iOS 页面间传值 之 单例传值 , block 传值
ios 页面间传值有许多,前边已经分享过属性传值和代理传值,今天主要说一下单例传值和 block 传值 单例传值:单例模式一种常用的开发的模式,单例因为在整个程序中无论在何时初始化对象,获取到的都是同 ...
- 百度:在O(1)空间复杂度范围内对一个数组中前后连段有序数组进行归并排序
一.题目理解 题目:数组al[0,mid-1]和al[mid,num-1]是各自有序的,对数组al[0,num-1]的两个子有序段进行merge,得到al[0,num-1]整体有序.要求空间复杂度为O ...
- sublime text 2 前端编码神器-快捷键与使用技巧介绍
介绍网址:http://www.xuanfengge.com/sublime-text-2-artifact.html
- FZU 2140 Forever 0.5
Problem 2140 Forever 0.5 Accept: 36 Submit: 113 Special JudgeTime Limit: 1000 mSec Memory ...
- JNI 多线程
一.概述 JNI编程和Linux上的C/C++编程还是挺相似的,每次java调用JNI中的函数时都会传入有关JVM的一些参数(如JNIEnv,jobject),每次JNI回调java中的方法时都要通过 ...
- easyui源码翻译1.32--Calendar(日历)
前言 前几天加班比较忙 未能及时更新翻译的 今天多发布几篇..下载该插件翻译源码 日历控件显示一个月的日历,允许用户选择日期和移动到下一个或上一个月.默认情况下,一周的第一天是周日.它可以通过设置'f ...
- IPv6 tutorial – Part 5: Address types and global unicast addresses
https://4sysops.com/archives/ipv6-tutorial-part-5-address-types-and-global-unicast-addresses/ In my ...