[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自动机的知识. 本题在构造文章的时 ...
随机推荐
- tlb,tlh,tli文件的关系
tlb文件:com类型库文件.在需要使用对应com类的模块里,“#import ...*.tlb”使用之. tlh.tli文件:他们是vc++编译器解析tlb文件生成的标准c++文件.因为odl和tl ...
- struts2中<s:property>的用法
1,访问Action值栈中的普通属性: <s:property value="attrName"/> 2,访问Action值栈中的对象属性(要有get set方法): ...
- ###C中的extern-static-const关键词
#@date: 2014-06-14 #@author: gerui #@email: forgerui@gmail.com Contents extern的作用一般是用来声音一个外部变量和函数.一般 ...
- iOS获取webview高度
int webHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.scr ...
- C# 并行开发总结
本文内容 均参考自 <C#并行高级编程> TPL 支持 数据并行(有大量数据要处理,必须对每个数据执行同样的操作, 任务并行(有好多可以并发运行的操作),流水线(任务并行和数据并行的结合体 ...
- jquery动态插入行
这是一个利用jquery动态插入输入内容的代码. html代码: <div title="分表2" class="ui-edit" style=" ...
- 用Python进行语音信号处理
1.语音信号处理之时域分析-音高追踪及其Python实现 2.语音信号处理之时域分析-音高及其Python实现 参考: 1.NumPy
- 373. Find K Pairs with Smallest Sums
You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. 给你两个数组n ...
- wait(...) notify() notifyAll()
简介 wait.notify.notifyAll是Java中3个与线程有关的方法,它们都是Object类中的方法. 其中,wait方法有3个重载形式: 1.wait() 2.wait(long tim ...
- 九度OJ 1534 数组中第K小的数字 -- 二分查找
题目地址:http://ac.jobdu.com/problem.php?pid=1534 题目描述: 给定两个整型数组A和B.我们将A和B中的元素两两相加可以得到数组C. 譬如A为[1,2],B为[ ...