题意:有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;//失败指针指向rootelse {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. AC自动机 - 多模式串的匹配 --- HDU 3695 Computer Virus on Planet Pandora

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

  2. HDU 2896 病毒侵袭 AC自己主动机题解

    本题是在text里面查找key word的增强版.由于这里有多个text. 那么就不能够简单把Trie的叶子标志记录改动成-1进行加速了,能够使用其它技术.我直接使用个vis数组记录已经訪问过的节点, ...

  3. Hdu 3341 Lost#39;s revenge (ac+自己主动机dp+hash)

    标题效果: 举个很多种DNA弦,每个字符串值值至1.最后,一个长字符串.要安排你最后一次另一个字符串,使其没事子值和最大. IDEAS: 首先easy我们的想法是想搜索的!管她3721..直接一个字符 ...

  4. hdu 2222 Keywords Search ac自己主动机

    点击打开链接题目链接 Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

  5. uva 11468 - Substring(AC自己主动机+概率)

    题目链接:uva 11468 - Substring 题目大意:给出一些字符和各自字符相应的选择概率.随机选择L次后得到一个长度为L的字符串,要求该字符串不包括随意一个子串的概率. 解题思路:构造AC ...

  6. POJ 1625 Censored! (AC自己主动机 + 高精度 + DP)

    题目链接:Censored! 解析:AC自己主动机 + 高精度 + 简单DP. 字符有可能会超过128.用map映射一下就可以. 中间的数太大.得上高精度. 用矩阵高速幂会超时,简单的DP就能解决时间 ...

  7. Codeforces 86C Genetic engineering (AC自己主动机+dp)

    题目大意: 要求构造一个串,使得这个串是由所给的串相连接构成,连接能够有重叠的部分. 思路分析: 首先用所给的串建立自己主动机,每一个单词节点记录当前节点可以达到的最长后缀. 開始的时候想的是dp[i ...

  8. POJ 2778 AC自己主动机+矩阵幂 不错的题

    http://poj.org/problem?id=2778 有空再又一次做下,对状态图的理解非常重要 题解: http://blog.csdn.net/morgan_xww/article/deta ...

  9. hdu 2243 考研路茫茫——单词情结(AC自动+矩阵)

    考研路茫茫--单词情结 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

最新文章

  1. centos7每天定时删除备份mysql文件
  2. arp 不同网段 相同vlan_三层交换机,相同的网段,不同的VLAN ,怎么通信?
  3. Swing中常用的方法
  4. js高级技巧之柯里化
  5. 卡通驱动项目ThreeDPoseTracker——模型驱动解析
  6. Spark SQL 1.3.0 DataFrame介绍、使用及提供了些完整的数据写入
  7. Windows Azure Cloud Service (41) 修改云服务IIS托管管道模式为4.0经典模式
  8. Linux保护线程,linux的线程是否受到了保护?
  9. android 环形进地图条,easyEcharts折线,柱状,饼图,仪表盘,环形,水球,圆柱,地图纯JS绘制...
  10. 数字信号处理(4)- 自适应滤波器
  11. Python 打印九九乘法表
  12. 关于MD5和salt盐值加密后破解方法
  13. Prometheus监控 Blackbox_exporter黑盒监测
  14. pandas学习笔记(十一):绘图(Plotting)
  15. 网络安全新晋网红“零信任”
  16. 112A.Petya and Strings
  17. 分列:将excel单元格的内容拆分为两列
  18. 网页中嵌入QQ和邮箱
  19. matlab批量导入excel表格数据,matlab导入excel表格数据-如何用matlab读取多个excel表格数据,将每个表格数......
  20. Jmeter获取短信验证码接口压测

热门文章

  1. mysql修改字段null为空字符串
  2. 手机端滚动屏幕加载更多
  3. 在Linux环境下mysql的root密码忘记解决方法 1.首先确认服务器出于安全的状态,也就是没有人能够任意地连接MySQL数据库。 2.修改MySQL的登录设置: # vi /etc/my.c
  4. 安装使用sublime 对比工具sublimerge
  5. 猫和老鼠服务器维护多久结束,猫和老鼠手游关服公告 告别是为了每次更好的遇见!...
  6. Spring boot logback的使用(日志记录)
  7. Spring Boot 全局异常机制
  8. python设计模式13-责任链模式
  9. python装饰器性能_python装饰器的特性原理详解
  10. windows10中git-bash闪退的解决办法