题意:给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的更多相关文章

  1. hdu 3695:Computer Virus on Planet Pandora(AC自动机,入门题)

    Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 256000/1280 ...

  2. 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 ...

  3. HDU 3695 Computer Virus on Planet Pandora(AC自动机模版题)

    Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 256000/1280 ...

  4. hdu ----3695 Computer Virus on Planet Pandora (ac自动机)

    Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 256000/1280 ...

  5. hdu 3695 Computer Virus on Planet Pandora(AC自己主动机)

    题目连接:hdu 3695 Computer Virus on Planet Pandora 题目大意:给定一些病毒串,要求推断说给定串中包括几个病毒串,包括反转. 解题思路:将给定的字符串展开,然后 ...

  6. HDU-3695 Computer Virus on Planet Pandora

    HDU-3695 Computer Virus on Planet Pandora 题意:电脑中病毒了, 现在n钟病毒指令, 然后有一个电脑指令, 看一下这个电脑指令中了几个病毒, 如果电脑种了某一个 ...

  7. HDU 3695 / POJ 3987 Computer Virus on Planet Pandora

      Computer Virus on Planet Pandora Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1353 ...

  8. 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 ...

  9. AC自动机 - 多模式串的匹配 --- HDU 3695 Computer Virus on Planet Pandora

    Problem's Link Mean: 有n个模式串和一篇文章,统计有多少模式串在文章中出现(正反统计两次). analyse: 好久没写AC自动机了,回顾一下AC自动机的知识. 本题在构造文章的时 ...

随机推荐

  1. SQL访问EXCEL错误集合

    --行集函数 --1, OPENDATASOURCE 环境:WIN7,SQL 2014,OFFICE 2013 SELECT * FROM OPENDATASOURCE('Microsoft.ACE. ...

  2. oc 通过webView调用js方法

    - (void)viewDidLoad { [super viewDidLoad]; //加载本地web页面 web = [[UIWebView alloc]init]; web.background ...

  3. 【我们都爱Paul Hegarty】斯坦福IOS8公开课个人笔记2 Xcode、Auto Layout及MVC

    原文链接不知道在哪, 接着上一话来讲,上一话中讲到了MVC,那么MVC在IOS8开发中是如何应用的呢?Paul Hegarty老师给我们展示了一个计算器的Demo,首先新建一个工程,老师把AppDel ...

  4. Objective-C 学习笔记(Day 3,上)

    ------------------------------------------- 类方法   ①类方法:        + 开头的方法(定义的过程形式和对象方法一样,只不过 + 开头,这是唯一的 ...

  5. Vue.js中Directive知识

    近期所学的Vue.js这个MVVM前端技术缓解了我一直愁于前后端开发杂糅所带来的痛苦.今天就来说说关于Vue.js里面的Directive知识. Directive Directive看上去虽然和An ...

  6. Linux操作系统是如何工作的?破解操作系统的奥秘

    学号:SA12**6112 研究笔记: 1:计算机是怎么样工作的 2:用户态到内核态切换之奥秘解析 3:进程切换之奥秘解析 本博文主要是根据前3篇笔记来总结Linux内核的工作机制. 一:操作系统工作 ...

  7. ExtJs 添加员工 实例 ---- 锚点布局 anchor 可自动伸缩

    代码如下: <script type="text/javascript"> Ext.onReady(function () { // 创建一条记录行, job 为 di ...

  8. Linux C 程序 数组(EIGHT)

    数组 1.一维数组的定义和使用,声明时数组默认值为0 int a[n]; 这样定义不合法,n是变量 ,数组规定[]里只能为常量 ] = {,,,,,,,,,}; a[] = {,} ;//部分赋值 , ...

  9. android xml产生和解析

    public static void writeToXml(Map<String, Object> map,Writer writer) throws Exception, Illegal ...

  10. 【转】C#中没有id 没有name C#怎么点击按钮

    HTML按钮元素 <input type="submit" value="确定" class="dialogbtn" C# 执行代码 ...