题意:给t、n,t个案例,n个字符串

  下面给n+1个字符串,n个互不相同的小串,最后一个是模式串

  模式串会出现[qx]的形式,q为数字,x为一个字母

问n个小串在模式串中出现的个数,正着出现、反着出现都算。

蛮裸的ac自动机,本来想着在query里面把找到过的end清零,把模式串展开正着反着找两遍,那即使再找到加零也不影响。但是超时了。。。

于是把找到过的end标为-1,-1了就不再找下去,就可以了~上代码

  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <climits>
  5. #include <cctype>
  6. #include <cmath>
  7. #include <string>
  8. #include <sstream>
  9. #include <iostream>
  10. #include <algorithm>
  11. #include <iomanip>
  12. using namespace std;
  13. #include <queue>
  14. #include <stack>
  15. #include <vector>
  16. #include <deque>
  17. #include <set>
  18. #include <map>
  19. typedef long long LL;
  20. typedef long double LD;
  21. const double eps=1e-;
  22. #define pi acos(-1.0)
  23. #define lson l, m, rt<<1
  24. #define rson m+1, r, rt<<1|1
  25. typedef pair<int, int> PI;
  26. typedef pair<int, PI> PP;
  27. #ifdef _WIN32
  28. #define LLD "%I64d"
  29. #else
  30. #define LLD "%lld"
  31. #endif
  32. //#pragma comment(linker, "/STACK:1024000000,1024000000")
  33. //LL quick(LL a, LL b){LL ans=1;while(b){if(b & 1)ans*=a;a=a*a;b>>=1;}return ans;}
  34. //inline int read(){char ch=' ';int ans=0;while(ch<'0' || ch>'9')ch=getchar();while(ch<='9' && ch>='0'){ans=ans*10+ch-'0';ch=getchar();}return ans;}
  35. //inline void print(LL x){printf(LLD, x);puts("");}
  36. //inline void read(double &x){char c = getchar();while(c < '0') c = getchar();x = c - '0'; c = getchar();while(c >= '0'){x = x * 10 + (c - '0'); c = getchar();}}
  37. //inline void sc(LL &x){scanf(LLD, &x);}
  38.  
  39. struct Trie
  40. {
  41. int next[][], fail[], end[];
  42. int root, L;
  43. int newnode()
  44. {
  45. for(int i=;i<;i++)
  46. next[L][i]=-;
  47. end[L++]=;
  48. return L-;
  49. }
  50. void init()
  51. {
  52. L=;
  53. root=newnode();
  54. }
  55. void insert(char buf[])
  56. {
  57. int len=strlen(buf);
  58. int now=root;
  59. for(int i=;i<len;i++)
  60. {
  61. if(next[now][buf[i]-'A']==-)
  62. next[now][buf[i]-'A']=newnode();
  63. now=next[now][buf[i]-'A'];
  64. }
  65. end[now]++;
  66. }
  67. void build()
  68. {
  69. queue<int> Q;
  70. fail[root]=root;
  71. for(int i=;i<;i++)
  72. if(next[root][i]==-)
  73. next[root][i]=root;
  74. else
  75. {
  76. fail[next[root][i]]=root;
  77. Q.push(next[root][i]);
  78. }
  79. while(!Q.empty())
  80. {
  81. int now=Q.front();
  82. Q.pop();
  83. for(int i=;i<;i++)
  84. if(next[now][i]==-)
  85. next[now][i]=next[fail[now]][i];
  86. else
  87. {
  88. fail[next[now][i]]=next[fail[now]][i];
  89. Q.push(next[now][i]);
  90. }
  91. }
  92. }
  93. int query(char buf[])
  94. {
  95. int len=strlen(buf);
  96. int now=root;
  97. int res=;
  98. for(int i=;i<len;i++)
  99. {
  100. now=next[now][buf[i]-'A'];
  101. int tmp=now;
  102. while(tmp!=root && end[tmp]!=-)
  103. {
  104. res+=end[tmp]; //printf("%d %d\n", tmp, end[tmp]);
  105. end[tmp]=-;
  106. tmp=fail[tmp];
  107. }
  108. }
  109. return res;
  110. }
  111. }ac;
  112. char buf[], tmp[];
  113. int main()
  114. {
  115. #ifndef ONLINE_JUDGE
  116. freopen("in.txt", "r", stdin);
  117. freopen("out.txt", "w", stdout);
  118. #endif
  119. int T;
  120. scanf("%d", &T);
  121. while(T--)
  122. {
  123. int n;
  124. scanf("%d", &n);
  125. ac.init();
  126. for(int i=;i<n;i++)
  127. {
  128. scanf("%s", buf);
  129. ac.insert(buf);
  130. // int LEN=strlen(buf);
  131. // reverse(buf, buf+LEN);
  132. // ac.insert(buf);
  133. }
  134. ac.build();
  135. scanf("%s", tmp);
  136. int LEN=strlen(tmp), d=;
  137. for(int i=;i<LEN;i++)
  138. if(tmp[i]=='[')
  139. {
  140. int cur=i+, num=;
  141. while(isdigit(tmp[cur]))
  142. num=num*+tmp[cur++]-'';
  143. fill(buf+d, buf+d+num, tmp[cur]);
  144. i=cur+, d+=num;
  145. }
  146. else
  147. buf[d++]=tmp[i];
  148. buf[d]='\0';
  149. // puts(buf);
  150. // for(int i=0;i<ac.L;i++)
  151. // printf("%d %d\n", i, ac.end[i]);
  152. int ans=ac.query(buf);
  153. reverse(buf, buf+d);
  154. printf("%d\n", ans+ac.query(buf));
  155. }
  156. return ;
  157. }

HDOJ 3695

[AC自动机]HDOJ3695 Computer Virus on Planet Pandora的更多相关文章

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

  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. hdu 3695 Computer Virus on Planet Pandora(AC自己主动机)

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

  6. HDU-3695 Computer Virus on Planet Pandora

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

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

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

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

  9. AC自动机 - 多模式串的匹配 --- HDU 3695 Computer Virus on Planet Pandora

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

随机推荐

  1. oracle拼接字段用||

    ① //dual相当于一个临时表.用来测量@@@@H210000000003I4R 的长度用length() select length('@@@@H210000000003I4R') from du ...

  2. 自己动手写一个简单的(IIS)小型服务器

    因为第一次在博客园发表随笔,不太会用,这个笔记是我之前在印象笔记中写好的,然后直接copy过来,有兴趣自己做一个IIS服务器的小伙伴们可以参照下面的流程做一次,也可以叫我要源代码,不过要做完,我觉得花 ...

  3. linux命令 common 文件比较

    比较已经排序的文件 comm [options] file1 file2 comm将逐行比较已经排序的两个文件.显示结果包括3列: 第1列为只在file1中找到的行;第2列为只在file2中找到的行; ...

  4. C# 编写短信发送Window服务

    我们做项目过程中,一般都会有发送短信的需求.最常见的就是户注册或者登录时发送短信验证码.不同类型的短信发送,我们都可以放到到一张短信表中,然后通过一个定时的作业去执行短信发送.而定时作业的执行,我们就 ...

  5. 解决某些手机RadioGroup中的RadioButton不居中的问题

    问题:RadioButton中使用android:gravity="center"使其图片文字居中,在我的华为荣耀7手机上居中显示了,但在HUAWEI G606-T00却显示在右侧 ...

  6. <转载>国外程序员推荐的免费编程书籍资源

    一.George Stocker 提供了一大串,分类如下: How to Design Programs: An Introduction to Computing and Programming 2 ...

  7. c++Builder 下的文件及目录操作

    转自 http://blog.csdn.net/ktcserver/article/details/936329 一.判断目录是否存在:           C++   Builder中提供了检查文件 ...

  8. Oracle PL/SQL 多重选择句

    Oracle中语句块的基本格式: declare --变量定义,初始化赋值. begin --变量的赋值,函数调用,if,while等. end: Oracle中的语句:关系运算符:= <> ...

  9. 访问图像中的像素[OpenCV 笔记16]

    再更一发好久没更过的OpenCV,不过其实写到这个部分对计算机视觉算法有所了解的应该可以做到用什么查什么了,所以后面可能会更的慢一点吧,既然开了新坑,还是机器学习更有研究价值吧... 图像在内存中的存 ...

  10. Poj 1503 Integer Inquiry

    1.链接地址: http://poj.org/problem?id=1503 2.题目: Integer Inquiry Time Limit: 1000MS   Memory Limit: 1000 ...