• 题意

    问一个字符串中包含多少种模式串,该字符串的反向串包含也算。
  • 思路

    解析一下字符串,简单。

    建自动机的时候,通过fail指针建立trie图。这样跑图的时候不再跳fail指针,相当于就是放弃了fail指针。

    跑的时候,已经走过的模式串不能二次计数。
  • 代码

    附赠一大波样例
#include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h>
using namespace std; const int maxn=5100010;
const int N=250010;
char cmp[maxn];
char dmp[maxn]; struct Dfa {
int trie[N][26],cnt;
int e[N];
int fail[N];
char ch; void init(char c) {
memset(trie,0,sizeof(trie));
memset(e,0,sizeof(e));
memset(fail,0,sizeof(fail));
cnt=0;
ch=c;
} void insert(char * s) {
int p=0;
for (int i=0;s[i];i++) {
int to=s[i]-ch;
if (trie[p][to]==0) {
trie[p][to]=++cnt;
}
p=trie[p][to];
}
e[p]++;
} void build() {
queue<int> q;
for (int i=0;i<26;i++) {
if (trie[0][i]) {
q.push(trie[0][i]);
}
} while (!q.empty()) {
int root=q.front();
q.pop();
for (int i=0;i<26;i++) {
if (trie[root][i]) {
fail[trie[root][i]]=trie[fail[root]][i];
q.push(trie[root][i]);
}
else {
trie[root][i]=trie[fail[root]][i];
}
}
}
} long long query(char * s) {
long long ans=0;
int p=0;
for (int i=0;s[i];i++) {
p=trie[p][s[i]-ch];//第一层的空节点k trie[0][k]=0
for (int j=p;j&&~e[j];j=fail[j]) {
ans+=e[j];
e[j]=-1;
}
}
int len=strlen(s);
p=0;
for (int i=len-1;i>=0;i--) {
p=trie[p][s[i]-ch];
for (int j=p;j&&~e[j];j=fail[j]) {
ans+=e[j];
e[j]=-1;
}
}
return ans;
}
}dfa; void translate(char * c)
{
int cnt=0,x=0;
bool flag=false;
for (int i=0;c[i];i++) {
if (c[i]!='['&&c[i]!=']') {
if (c[i]>='A'&&c[i]<='Z') {
if (flag==false) {
dmp[cnt++]=c[i];
}
else {
while (x--) {
dmp[cnt++]=c[i];
}
x=0;
flag=false;
}
}
else {
flag=true;
x=x*10+c[i]-'0';
}
}
}
dmp[cnt]='\0';
//cout<<dmp<<endl;
} int main()
{
int T,n;
char virus[1010];
scanf("%d",&T);
while (T--) {
dfa.init('A');
scanf("%d",&n);
for (int i=0;i<n;i++) {
scanf("%s",virus);
dfa.insert(virus);
}
dfa.build();
scanf("%s",cmp);
translate(cmp);
printf("%lld\n",dfa.query(dmp));
}
return 0;
}
/*
5
3
AB
BH
H
ABH
3
AB
AD
B
ABADB
3
IMQ
MQ
Q
QMI
2
ABB
BD
ABBD
5
ABB
BD
BB
DE
BDE
EDBBA
*/

POJ 3987 Computer Virus on Planet Pandora (AC自动机优化)的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

  9. HDU-3695 Computer Virus on Planet Pandora

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

随机推荐

  1. 进程管理 supervisor

    背景知识: supervisor是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动重启. 它是通过fork/exec的方式把这 ...

  2. 钉钉内网穿透工具在windows的使用。

    钉钉内网穿透工具在windows环境下使用 1.WIN+R,然后cmd,调出dos控制台 2.进入内网穿透程序ding.exe所在目录 3.执行 ./ding.exe -config=ding.cfg ...

  3. 512,a标签的target属性

    <a> 标签的 target 属性规定在何处打开链接文档. 1.“_blank”的意思: 浏览器总在一个新打开.未命名的窗口中载入目标文档. 2.“_parent”的意思: 这个目标,使得 ...

  4. centos6 源码编译安装nginx 1.6 教程 nginx安装脚本

    操作系统centos 6.9 安装nginx需要pcre zlib openssl的库,下文都是在官网直接下载用作编译安装 该nginx安装教程,有安装maxmind IP 库 该教材有修改最大打开文 ...

  5. 【C语言】输入三个正整数a,b,c,求最大值,要求定义一个计算最大值的函数max(a,b),返回a,b的值

    #include<stdio.h> int max(int a, int b)/*定义函数*/ { if (a > b) return a; else return b; } int ...

  6. 使用maven构建项目时,SSM和springboot项目的打包与云服务器部署

    下面讲讲如何打包SSM和springboot项目,并部署到云服务器上. 由于使用的IDE不同,有的使用eclipse,有的使用idea,所以如果在IDE中按照 maven clean 再 maven ...

  7. THINKPHP 模板上传图片--后台接收图片

    模板 {extend name="public/base" /} {block name="body"} <div class="row&quo ...

  8. 前端知识之css

    css的几种引入方式 行内样式 行内式是在标记的style属性中设定css样式,不推荐大规模使用 <p style='color:red'>hello world</p> 内部 ...

  9. TCP和UDP的一些注意事项

    TCP的一些注意事项 1. tcp服务器一般情况下都需要绑定,否则客户端找不到这个服务器,更无法链接到服务器 2. tcp客户端一般不绑定,因为是主动链接服务器,所以只要确定好服务器的ip.port等 ...

  10. Network - HTTP 可能遇到的状态码

    1. 概述 工作中, 会遇到 形形色色 的状态码 虽然这些状态码, 很多都是自定义的, 但是很多的 自定义, 还是有些 约定俗成 的规律 状态 虽然现在发布了, 但是基本算是没有, 还得慢慢补充 2. ...