题意:有n种病毒序列(字符串),一个模式串,问这个字符串包括几种病毒。

包括相反的病毒也算。字符串中[qx]表示有q个x字符。具体见案列。

0 < q <= 5,000,000尽然不会超,无解

3
2
AB
DCB
DACB
3
ABC
CDE
GHI
ABCCDEFIHG
4
ABB
ACDEE
BBB
FEEE
A[2B]CD[4E]F
 
Sample Output
0
3
2
Hint
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’.

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<string>
#include<queue>
#include<math.h>
#include<algorithm>
#include<iostream>
using namespace std;
const int kind = 26;
const int maxn = 250*1000; //注意RE,单词长度*单词个数
const int M = 5100000;
struct node
{
node *fail;
node *next[kind];
int count;
node()
{
fail = NULL;
count = 0;
memset(next,0,sizeof(next));
}
}*q[maxn];
char keyword[1010],str[M],str1[M];
int head,tail;
void insert(char *str,node *root)
{
node *p=root;
int i=0,index;
while(str[i])
{
index = str[i] - 'A';
if(p->next[index]==NULL)
p->next[index] = new node();
p = p->next[index];
i++;
}
p->count++;
} void build_ac(node *root)
{
int i;
root->fail=NULL;
q[head++]=root;
while(head!=tail)
{
node *temp = q[tail++];
node *p=NULL;
for(i=0;i<26;i++)
{
if(temp->next[i]!=NULL)
{
if(temp==root)
temp->next[i]->fail=root;//失败指针指向root
else
{
p = temp->fail;
while(p!=NULL)
{
if(p->next[i]!=NULL)
{
temp->next[i]->fail=p->next[i];
break;
}
p=p->fail;
}
if(p==NULL)
temp->next[i]->fail=root;
}
q[head++]=temp->next[i];
}
}
}
} int query(node *root)
{
int i=0,cnt=0,index,len=strlen(str);
node *p=root;
while(str[i])
{
index = str[i]-'A';
while(p->next[index]==NULL&&p!=root)
p = p->fail;
p=p->next[index];
p=(p==NULL)?root:p;
node *temp = p;
while(temp!=root&&temp->count!=-1)//沿失配边走,走过的不走
{
cnt+=temp->count;
temp->count=-1;
temp=temp->fail;
}
i++;
}
return cnt;
} int value(int p,int q)
{
int i,ans=0,w=1;
for (i=q;i>=p;i--)
{
ans+=(str1[i]-'0')*w;
w*=10;
} return ans;
}
int main()
{
int n,t;
scanf("%d",&t);
while(t--)
{
head=tail=0;
node *root = new node();
scanf("%d",&n);
while(n--)
{
scanf("%s",keyword);
insert(keyword,root);
}
build_ac(root);
scanf("%s",str1);
int l=strlen(str1),i,j,k; j=0;
for (i=0;i<l;)
{
if (str1[i]!='[') str[j++]=str1[i++];
else
{
/*for (k=i+1;str1[k]!=']';k++);
int v=0,w=1;
for (int l=k-2;l>=i+1;l--)
{
v+=(str1[l]-'0')*w;
w*=10;
} */
int v=0;
i++;
while(str1[i]>='0'&&str1[i]<='9')
{
v=v*10+str1[i]-'0';
i++;
}
for (int k1=1;k1<=v;k1++) str[j++]=str1[i]; i+=2;
}
}
str[j]='\0';
//printf("%s\n",str); int h=query(root);
char chh; l=strlen(str);
for (i=0;i<=(l-1)/2;i++)
{
chh=str[l-i-1];
str[l-i-1]=str[i];
str[i]=chh;
}
//printf("%s",str);
h+=query(root);
printf("%d\n",h);
}
return 0;
}
/*
3
2
AB
DCB
DACB
3
ABC
CDE
GHI
ABCCDEFIHG
4
ABB
ACDEE
BBB
FEEE
A[2B]CD[4E]F
*/

HDU 3695 Computer Virus on Planet Pandora (AC自己主动机)的更多相关文章

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

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

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

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

  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. AC自动机 - 多模式串的匹配 --- HDU 3695 Computer Virus on Planet Pandora

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

  6. HDU3695 - Computer Virus on Planet Pandora(AC自动机)

    题目大意 给定一个文本串T,然后给定n个模式串,问有多少个模式串在文本串中出现,正反都可以 题解 建立好自动机后.把文本串T正反各匹配一次,刚开始一直TLE...后面找到原因是重复的子串很多以及有模式 ...

  7. POJ 3987 Computer Virus on Planet Pandora (AC自动机优化)

    题意 问一个字符串中包含多少种模式串,该字符串的反向串包含也算. 思路 解析一下字符串,简单. 建自动机的时候,通过fail指针建立trie图.这样跑图的时候不再跳fail指针,相当于就是放弃了fai ...

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

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

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

随机推荐

  1. vue2 使用 element-ui

    看了  http://element.eleme.io/#/zh-CN/component/installation     一些组件和样式够用了 , 试了下 element-ui ,配合到vue中 ...

  2. 结合java的反射和泛型性质简化JDBC和相应的同步等服务器数据库操作代码

    github地址:https://github.com/hzphzp/HeartTrace_Server 我们的服务器端数据库并没有用sqllite, 而是直接用mysql,并且用JDBC直接进行操作 ...

  3. 阅读《大道至简第一章》读后感 (java 伪代码)

         通读大道至简第一章愚公移山,可以将其看做一个完整的工程,首先是创建工程的原因,需求:“惩山北之塞,出入之迂”,而后是团队之间的商议:“聚室而谋曰”,然后确定工程的目标:“毕力平险,指通豫南, ...

  4. python中的内容编码

    一.python编码简介 1)编码格式简介 python解释器在加载 .py 文件中的代码时,会对内容进行编码(默认ASCII),ASCII(American Standard Code for In ...

  5. vue学习(一)、Vue.js简介

    Vue.js 五天 汤小洋一. Vue.js简介1. Vue.js是什么Vue.js也称为Vue,读音/vju:/,类似view,错误读音v-u-e 版本:v1.0 v2.0 是一个构建用户界面的框架 ...

  6. Linux常用命令(二)————压缩+解压

    tar -c: 建立压缩档案-x:解压-t:查看内容-r:向压缩归档文件末尾追加文件-u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个.下面的 ...

  7. C# 按位或,按位与, 按位异或

    a != b  ----->  a = a | b  , a 或者 b 只要有一个为 1, 那么,a 的最终结果就为 1 a &= b  ----->  a = a & b ...

  8. Linux中如何安装loadgenerator

    /* 1. 到官方网站到HP官网下载Load Generator 安装文件 _Load_Generator_11.00_T7330-15010.iso或者其它网站下载loadrunner-11-loa ...

  9. 整合VIM和Graphviz,并且使用本办法实现实时预览

    在编程或是整理知识的时候一直苦于没有一款可以帮助理清思路的工具. 在网上苦寻良久,终于找到了一款可心可意的小软件 —— Graphviz. 折腾了一番,终于可以凑合着用了. 现将折腾的成果记录于此以作 ...

  10. codeforces 803G Periodic RMQ Problem

    codeforces 803G Periodic RMQ Problem 题意 长度为\(1e5\)的数组复制\(1e4\)次,对新的数组进行区间覆盖和区间最小值查询两种操作,操作次数\(1e5\). ...