题意:略

思路:套用ac自动机模板

  1. #include <iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. using namespace std;
  5. char str[11000];
  6. int total,head,tail;
  7. struct node{
  8. node *next[130];
  9. node *fail;
  10. int cnt,id;
  11. node(){
  12. fail=NULL;
  13. cnt=0;
  14. memset(next,NULL,sizeof(next));
  15. }
  16. }*q[500000];
  17. void insert(node *root,int id){
  18. node *p=root;
  19. int i=0,index;
  20. while(str[i]){
  21. index=str[i]-31;
  22. if(p->next[index]==NULL) p->next[index]=new node();
  23. p=p->next[index];
  24. i++;
  25. }
  26. p->cnt++;
  27. p->id=id;
  28. }
  29. void build_ac(node *root){
  30. root->fail=NULL;
  31. q[head++]=root;
  32. node *p=NULL;
  33. while(head!=tail){
  34. node *temp = q[tail++];
  35. for(int i=0;i<=128;i++){
  36. if(temp->next[i]!=NULL){
  37. if(temp==root) temp->next[i]->fail=root;
  38. else{
  39. p=temp->fail;
  40. while(p!=NULL){
  41. if(p->next[i]!=NULL){
  42. temp->next[i]->fail=p->next[i];
  43. break;
  44. }
  45. p=p->fail;
  46. }
  47. if(p==NULL) temp->next[i]->fail=root;
  48. }
  49. q[head++]=temp->next[i];
  50. }
  51. }
  52. }
  53. }
  54. bool ans[1000];
  55. int cnt;
  56. int query(node *root){
  57. int i=0,index;
  58. node *p=root;
  59. while(str[i]){
  60. index=str[i]-31;
  61. while(p->next[index]==NULL&&p!=root) p=p->fail;
  62. p=p->next[index];
  63. p=(p==NULL)?root:p;
  64. node *temp=p;
  65. while(temp!=root&&temp->cnt>0){
  66. if(ans[temp->id]==false)
  67. cnt++,ans[temp->id]=true;
  68. temp=temp->fail;
  69. }
  70. i++;
  71. }
  72. }
  73. int main(int argc, char** argv) {
  74. int n,m,total,i,j;
  75. while(scanf("%d",&n)!=EOF){
  76. node *root=new node();
  77. head=tail=total=0;
  78. for(i=0;i<n;i++){
  79. scanf("%s",str);
  80. insert(root,i+1);
  81. }
  82. scanf("%d",&m);
  83. build_ac(root);
  84. for(i=1;i<=m;i++){
  85. memset(ans,false,sizeof(ans));
  86. cnt=0;
  87. scanf("%s",str);
  88. query(root);
  89. if(cnt>0){
  90. printf("web %d:",i);
  91. for(j=1;j<=500;j++)
  92. if(ans[j]==1)
  93. printf(" %d",j);
  94. printf("\n");
  95. total++;
  96. }
  97. }
  98. printf("total: %d\n",total);
  99. }
  100. return 0;
  101. }

hdu 2896 病毒侵袭_ac自动机的更多相关文章

  1. hdu 2896 病毒侵袭 ac自动机

    /* hdu 2896 病毒侵袭 ac自动机 从题意得知,模式串中没有重复的串出现,所以结构体中可以将last[](后缀链接)数组去掉 last[]数组主要是记录具有相同后缀模式串的末尾节点编号 .本 ...

  2. hdu 2896 病毒侵袭 AC自动机(查找包含哪些子串)

    病毒侵袭 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  3. hdu 2896 病毒侵袭 AC自动机 基础题

    病毒侵袭 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  4. HDU 2896 病毒侵袭 (AC自己主动机)

    pid=2896">http://acm.hdu.edu.cn/showproblem.php?pid=2896 病毒侵袭 Time Limit: 2000/1000 MS (Java ...

  5. HDU 2896 病毒侵袭(AC自动机水)

    病毒侵袭 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  6. HDU 2896 病毒侵袭(AC自动机)

    病毒侵袭 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  7. HDU 2896 病毒侵袭【AC自动机】

    <题目链接> Problem Description 当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻....在这样的时刻,人们却异常兴奋——我们能在有生之年看到500年一 ...

  8. HDU 2896 病毒侵袭 【AC自动机】

    HDU 2222 仅仅求出了和文本串匹配的模式串个数,本题要求求出匹配的模式串的编号. 不同的部分在代码中的注释部分. #include <cstdio> #include <cst ...

  9. HDU 2896 病毒侵袭 (AC自动机)

    这题模板题.............但是竟然要去重........调试了半天才发现.................... #include <cstdio> #include <i ...

随机推荐

  1. Palindrome Partitioning 解答

    Question Given a string s, partition s such that every substring of the partition is a palindrome. R ...

  2. Hive 3、Hive 的安装配置(本地derby模式)

    这种方式是最简单的存储方式,只需要在hive-site.xml做如下配置便可; $ vim hive-site.xml <configuration>   <property> ...

  3. WPF可视化控件打印

    Introduction While coding an application that displays a detailed report in a ScrollViewer, it was d ...

  4. VS快捷编码方式

    概念: 代码段是将预先定义好的可重用代码块快速插入到代码文件中,代码段提高了开发效率,增强了代码的可重用性:既节约了时间,又实现了不同开发人员间代码的共享.同时也可保证同一项目中代码风格的统一.   ...

  5. POJ-1118(超时,但未找到原因)

    #include<iostream> #include<map> #include<vector> using namespace std; //y=kx+z ty ...

  6. Oracle监听静态注册和动态注册

    静态注册和动态注册总结 一.什么是注册? 注册就是将数据库作为一个服务注册到监听程序.客户端不需要知道数据库名和实例名,只需要知道该数据库对外提供的服务名就可以申请连接到数据库.这个服务名可能与实例名 ...

  7. Oracle11g x64使用Oracle SQL Developer报错:Unable to find a Java Virtual Machine

    原因oracle 11g中安装的Oracle SQL Developer是32位的,而我们现在给他指定的java.exe却是64位的,所以会出现这种错误.解决方法1)从网上下载Oracle SQL D ...

  8. HTTP协议4之缓存--转

    HTTP协议提供了非常强大的缓存机制, 了解这些缓存机制,对提高网站的性能非常有帮助. 缓存的概念 缓存这个东西真的是无处不在, 有浏览器端的缓存, 有服务器端的缓存,有代理服务器的缓存, 有ASP. ...

  9. django Model模型二及Model模型对数据库的操作

    在django模型中负责与数据库交互的为Model层,Model层提供了一个基于orm的交互框架 一:创建一个最基本的Model from __future__ import unicode_lite ...

  10. PHP学习笔记十五【面向对象二】

    <?php class Cat{ //public 访问修饰符 public $name; public $age; } //创建 $cat1=new Cat; $cat1->name=& ...