[AC自动机]HDOJ3695 Computer Virus on Planet Pandora
题意:给t、n,t个案例,n个字符串
下面给n+1个字符串,n个互不相同的小串,最后一个是模式串
模式串会出现[qx]的形式,q为数字,x为一个字母
问n个小串在模式串中出现的个数,正着出现、反着出现都算。
蛮裸的ac自动机,本来想着在query里面把找到过的end清零,把模式串展开正着反着找两遍,那即使再找到加零也不影响。但是超时了。。。
于是把找到过的end标为-1,-1了就不再找下去,就可以了~上代码
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cctype>
#include <cmath>
#include <string>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <set>
#include <map>
typedef long long LL;
typedef long double LD;
const double eps=1e-;
#define pi acos(-1.0)
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
typedef pair<int, int> PI;
typedef pair<int, PI> PP;
#ifdef _WIN32
#define LLD "%I64d"
#else
#define LLD "%lld"
#endif
//#pragma comment(linker, "/STACK:1024000000,1024000000")
//LL quick(LL a, LL b){LL ans=1;while(b){if(b & 1)ans*=a;a=a*a;b>>=1;}return ans;}
//inline int read(){char ch=' ';int ans=0;while(ch<'0' || ch>'9')ch=getchar();while(ch<='9' && ch>='0'){ans=ans*10+ch-'0';ch=getchar();}return ans;}
//inline void print(LL x){printf(LLD, x);puts("");}
//inline void read(double &x){char c = getchar();while(c < '0') c = getchar();x = c - '0'; c = getchar();while(c >= '0'){x = x * 10 + (c - '0'); c = getchar();}}
//inline void sc(LL &x){scanf(LLD, &x);} struct Trie
{
int next[][], fail[], end[];
int root, L;
int newnode()
{
for(int i=;i<;i++)
next[L][i]=-;
end[L++]=;
return L-;
}
void init()
{
L=;
root=newnode();
}
void insert(char buf[])
{
int len=strlen(buf);
int now=root;
for(int i=;i<len;i++)
{
if(next[now][buf[i]-'A']==-)
next[now][buf[i]-'A']=newnode();
now=next[now][buf[i]-'A'];
}
end[now]++;
}
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]);
}
}
}
int query(char buf[])
{
int len=strlen(buf);
int now=root;
int res=;
for(int i=;i<len;i++)
{
now=next[now][buf[i]-'A'];
int tmp=now;
while(tmp!=root && end[tmp]!=-)
{
res+=end[tmp]; //printf("%d %d\n", tmp, end[tmp]);
end[tmp]=-;
tmp=fail[tmp];
}
}
return res;
}
}ac;
char buf[], tmp[];
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int T;
scanf("%d", &T);
while(T--)
{
int n;
scanf("%d", &n);
ac.init();
for(int i=;i<n;i++)
{
scanf("%s", buf);
ac.insert(buf);
// int LEN=strlen(buf);
// reverse(buf, buf+LEN);
// ac.insert(buf);
}
ac.build();
scanf("%s", tmp);
int LEN=strlen(tmp), d=;
for(int i=;i<LEN;i++)
if(tmp[i]=='[')
{
int cur=i+, num=;
while(isdigit(tmp[cur]))
num=num*+tmp[cur++]-'';
fill(buf+d, buf+d+num, tmp[cur]);
i=cur+, d+=num;
}
else
buf[d++]=tmp[i];
buf[d]='\0';
// puts(buf);
// for(int i=0;i<ac.L;i++)
// printf("%d %d\n", i, ac.end[i]);
int ans=ac.query(buf);
reverse(buf, buf+d);
printf("%d\n", ans+ac.query(buf));
}
return ;
}
HDOJ 3695
[AC自动机]HDOJ3695 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 Computer Virus on Planet Pandora (ac自动机)
Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 256000/1280 ...
- hdu 3695 Computer Virus on Planet Pandora(AC自己主动机)
题目连接:hdu 3695 Computer Virus on Planet Pandora 题目大意:给定一些病毒串,要求推断说给定串中包括几个病毒串,包括反转. 解题思路:将给定的字符串展开,然后 ...
- HDU-3695 Computer Virus on Planet Pandora
HDU-3695 Computer Virus on Planet Pandora 题意:电脑中病毒了, 现在n钟病毒指令, 然后有一个电脑指令, 看一下这个电脑指令中了几个病毒, 如果电脑种了某一个 ...
- 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 ...
- AC自动机 - 多模式串的匹配 --- HDU 3695 Computer Virus on Planet Pandora
Problem's Link Mean: 有n个模式串和一篇文章,统计有多少模式串在文章中出现(正反统计两次). analyse: 好久没写AC自动机了,回顾一下AC自动机的知识. 本题在构造文章的时 ...
随机推荐
- HTML5吧!少年
一.为了能使IE9以下的IE浏览器也能支持html5的标签,所以首先得在文档头部用条件注释的方法引入那段著名的代码. 1 2 3 <!--[if lt IE 9]> <script ...
- c#结束winword.exe进程、
最近在做一个c#生成word的功能.调用了微软自带的COM组件. 生成完以后发现有一个winword.exe无法关闭.调试或修改代码都没有搞明白. 遂强制关闭进程了. System.Diagnost ...
- THP Transparent HugePages关闭
ambari 安装Hortonworks HDP 时在检测host异常 The following hosts have Transparent Huge Pages (THP) enabled.TH ...
- HW—字符串最后一个单词的长度,单词以空格隔开。
描述 计算字符串最后一个单词的长度,单词以空格隔开. 知识点 字符串,循环 运行时间限制 0M 内存限制 0 输入 一行字符串,长度小于128. 输出 整数N,最后一个单词的长度. 样例输入 hell ...
- 判断手机还是PC浏览器
function goPAGE() { if ((navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobi ...
- 九度OJ 1385 重建二叉树
题目地址:http://ac.jobdu.com/problem.php?pid=1385 题目描述: 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都 ...
- BootstrapDialog点击空白处禁止关闭
在乐学一百的项目当中引用到了BootstrapDialog,其中后台发送短信时,为了防止管理员编辑了半天的短息,突然间因为点击某个空白区域导致丢失,所以在此禁用掉点击空白关闭弹出框. 主要属性为: c ...
- 数据库之mysql 视图
视图,把基本表的某些数据组合起来构成一个虚拟表的一种形式,之所以叫虚拟,是因为只有当视图用于一条语句中的时候,它才能存在.同时如果对视图中的数据进行修改,会同时修改到基本表中的数据. 创建视图: cr ...
- Entity Framework多对多关联映射的实现
Entity Framework是微软官方提供的一个ORM解决方案,它有纯正的血统,比NHibernate更容易使用并且与现有其它官方框架配合更加密切. 时代不断的在发展变化,记得10年前还是ADO( ...
- file_put_contents() 图片保存 函数成功之后返回值
今天弄图片保存时,用到file_put_contents()来保存图片,运行了几次,发下一直没有数据出来,以为是这个函数没操作成功 于是查看了下这个函数的用法和返回值,发现我输出的返回都正确,后来才发 ...