题目大意

给定一个文本串T,然后给定n个模式串,问有多少个模式串在文本串中出现,正反都可以

题解

建立好自动机后。把文本串T正反各匹配一次,刚开始一直TLE。。。后面找到原因是重复的子串很多以及有模式串是另外一个模式串的子串这种情况也很多~~~,所以我们用数组标记一下就好了~~~改了交上去之后是WA,最后发现时visit数组初始化错地方了。。。。

代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <string>
#include <queue>
using namespace std;
const int maxnode=500005;
const int sigma_size=26;
const int MAXN=5100005;
char T[MAXN],s[1005];
bool visit[maxnode];
int ans;
struct Triegragh
{
int ch[maxnode][sigma_size];
int fail[maxnode];
int val[maxnode];
int last[maxnode];
int sz;
void init()
{
memset(ch[0],0,sizeof(ch[0]));
memset(visit,false,sizeof(visit));
sz=1;
}
int idx(char c)
{
return c-'A';
}
void insert(char *s)
{
int u=0,n=strlen(s);
for(int i=0; i<n; i++)
{
int c=idx(s[i]);
if(!ch[u][c])
{
memset(ch[sz],0,sizeof(ch[sz]));
val[sz]=0;
ch[u][c]=sz++;
}
u=ch[u][c];
}
val[u]++;
}
void getfail()
{
queue<int>q;
fail[0]=0;
for(int c=0; c<sigma_size; c++)
{
int u=ch[0][c];
if(u)
{
fail[u]=0;
q.push(u);
last[u]=0;
}
}
while(!q.empty())
{
int r=q.front();
q.pop();
for(int c=0; c<sigma_size; c++)
{
int u=ch[r][c];
if(!u)
{
ch[r][c]=ch[fail[r]][c];
continue;
}
fail[u]=ch[fail[r]][c];
q.push(u);
last[u]=val[fail[u]]?fail[u]:last[fail[u]];
}
}
}
void count(int j)
{
if(j)
{
if(!visit[j]) ans++;
else return;
visit[j]=true;
count(last[j]);
}
}
void find(char *T)
{
int n=strlen(T);
int j=0;
for(int i=0; i<n; i++)
{
int c=idx(T[i]);
j=ch[j][c];
if(val[j])
count(j);
else if(last[j]) count(last[j]);
}
}
};
Triegragh ac;
int main()
{
int kase;
scanf("%d",&kase);
while(kase--)
{
int n;
scanf("%d",&n);
ac.init();
for(int i=1; i<=n; i++)
{
scanf("%s",s);
ac.insert(s);
}
ac.getfail();
getchar();
char c;
int j=0;
while(c=getchar(),c!='\n')
{
if(c=='[')
{
int m;
scanf("%d",&m);
c=getchar();
for(int i=0; i<m; i++)
T[j++]=c;
getchar();
}
else T[j++]=c;
}
T[j]='\0';
ans=0;
ac.find(T);
reverse(T,T+strlen(T));
ac.find(T);
printf("%d\n",ans);
}
return 0;
}

HDU3695 - Computer Virus on Planet Pandora(AC自动机)的更多相关文章

  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. POJ 3987 Computer Virus on Planet Pandora (AC自动机优化)

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

  3. HDU-3695 Computer Virus on Planet Pandora

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. Python多线程学习笔记

    Python中与多线程相关的模块有 thread, threading 和 Queue等,thread 和threading模块允许程序员创建和管理线程.thread模块提供了基本的线程和锁的支持,而 ...

  2. hdu 4550 卡片游戏 贪心

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4550 题意:有n(n <= 100)个0~9之间的卡片,从左往右将卡片放到之前的卡片最左边或者最 ...

  3. Java集合Map接口与Map.Entry学习

    Java集合Map接口与Map.Entry学习 Map接口不是Collection接口的继承.Map接口用于维护键/值对(key/value pairs).该接口描述了从不重复的键到值的映射. (1) ...

  4. sum_series() 求一列数的指定个数的数和(5个数字的和)

    #include <stdio.h> #include <stdarg.h> /*用sum_series() 求一列数的指定个数的数和(5个数字的和)*/ double sum ...

  5. Ubuntu下使用ap-hotspot出现“Another process is already running"问题的解决方案

    参考Problem with ap-hotspot 问题描述: This is the message displayed in my terminal screen when I typed sud ...

  6. 对CURL的一些研究

    http://www.kuqin.com/article/23candcplusplus/586014.html 前两天看到有人求客户端socket 发HTTP包的代码,受flw版主启发找了一些per ...

  7. php smarty section使用

    文件:section.tpl <html> <head> <title></title> </head> <body> {sec ...

  8. 使用JProfiler进行内存分析

    在最近的工作中,通过JProfiler解决了一个内存泄漏的问题,现将检测的步骤和一些分析记录下来,已备今后遇到相似问题时可以作为参考. 运行环境: Tomcat6,jdk6,JProfiler8 内存 ...

  9. char 和 varchar

    固定长度或可变长度的字符数据类型. char [ ( n ) ] 固定长度,非 Unicode 字符数据,长度为 n 个字节.n 的取值范围为 1 至 8,000,存储大小是 n 个字节.char 的 ...

  10. LinuxShell_variable+if+while

    [root@ossec-server mybash]# vim ./hello.sh #! /bin/sh # This is a example bash script echo "Hel ...