题目链接:Searching the String

解析:给一个长串。给n个不同种类的短串。问分别在能重叠下或者不能重叠下短串在长串中出现的次数。

能重叠的已经是最简单的AC自己主动机模板题了。

不能重叠的记录一下每一个匹配的串的起始位置保证不重叠就可以。

AC代码:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct Trie{
  5. int next[600010][26], fail[600010], deep[600010];
  6. int root, L;
  7.  
  8. int newnode(){
  9. for(int i = 0; i < 26; i++) next[L][i] = -1;
  10. L ++;
  11. return L-1;
  12. }
  13.  
  14. void init(){
  15. L = 0;
  16. root = newnode();
  17. deep[root] = 0;
  18. }
  19.  
  20. int insert(char buf[]){
  21. int len = strlen(buf);
  22. int now = root;
  23. for(int i = 0; i < len; i++){
  24. if(next[now][ buf[i] - 'a' ] == -1){
  25. next[now][ buf[i] - 'a' ] = newnode();
  26. deep[next[now][buf[i] - 'a']] = i+1;
  27. }
  28. now = next[now][ buf[i] - 'a' ];
  29. }
  30. return now;
  31. }
  32.  
  33. void build(){
  34. queue<int> Q;
  35. fail[root] = root;
  36. for(int i = 0; i < 26; i++){
  37. if(next[root][i] == -1) next[root][i] = root;
  38. else{
  39. fail[ next[root][i] ] = root;
  40. Q.push(next[root][i]);
  41. }
  42. }
  43. while(!Q.empty()){
  44. int now = Q.front();
  45. Q.pop();
  46. for(int i = 0; i < 26; i++){
  47. if(next[now][i] == -1) next[now][i] = next[ fail[now] ][i];
  48. else{
  49. fail[ next[now][i] ] = next[ fail[now] ][i];
  50. Q.push(next[now][i]);
  51. }
  52. }
  53. }
  54. }
  55.  
  56. int cnt[600010][2]; //cnt[][0]表示能重叠的,cnt[][1]表示不能重叠的
  57. int last[600010]; //上次匹配的字符
  58. void query(char buf[]){
  59. memset(cnt, 0, sizeof(cnt));
  60. memset(last, -1, sizeof(last));
  61. int len = strlen(buf);
  62. int now = root;
  63. for(int i = 0; i < len; i++){
  64. now = next[now][ buf[i] - 'a' ];
  65. int temp = now;
  66. while(temp != root){
  67. cnt[temp][0] ++;
  68. if(i - last[temp] >= deep[temp]){ //保证不重叠
  69. last[temp] = i;
  70. cnt[temp][1] ++;
  71. }
  72. temp = fail[temp];
  73. }
  74. }
  75. }
  76. };
  77.  
  78. char buf[20];
  79. char str[100010];
  80. int typ[100010], pos[100010];
  81. Trie ac;
  82.  
  83. int main(){
  84. #ifdef sxk
  85. freopen("in.txt", "r", stdin);
  86. #endif //sxk
  87.  
  88. int n;
  89. int kase = 0;
  90. while(scanf("%s", str) == 1){
  91. scanf("%d", &n);
  92. ac.init();
  93. for(int i = 0; i < n; i++){
  94. scanf("%d%s", &typ[i], buf);
  95. pos[i] = ac.insert(buf);
  96. }
  97. ac.build();
  98. ac.query(str);
  99. printf("Case %d\n", ++kase);
  100. for(int i=0; i<n; i++)
  101. printf("%d\n", ac.cnt[pos[i]][typ[i]]);
  102. printf("\n");
  103. }
  104. return 0;
  105. }

ZOJ 3228 Searching the String (AC自己主动机)的更多相关文章

  1. ZOJ - 3228 Searching the String (AC自己主动机)

    Description Little jay really hates to deal with string. But moondy likes it very much, and she's so ...

  2. ZOJ 3228 Searching the String(AC自动机)

    Searching the String Time Limit: 7 Seconds      Memory Limit: 129872 KB Little jay really hates to d ...

  3. ZOJ 3494 BCD Code (AC自己主动机 + 数位DP)

    题目链接:BCD Code 解析:n个病毒串.问给定区间上有多少个转换成BCD码后不包括病毒串的数. 很奇妙的题目. . 经典的 AC自己主动机 + 数位DP 的题目. 首先使用AC自己主动机,得到b ...

  4. zoj 3228:Searching the String

    Description Little jay really hates to deal with string. But moondy likes it very much, and she's so ...

  5. Zoj 3545 Rescue the Rabbit(ac自己主动机+dp)

    标题效果: 鉴于DNA有一个正确的顺序值.请构造一个长度I的DNA在这个序列使DNA正确的顺序值极大.它被认为是负的输出噼啪. .. IDEAS: 施工顺序是,ac己主动机上走,求最大要用到dp dp ...

  6. zoj 3430 Detect the Virus(AC自己主动机)

    题目连接:zoj 3430 Detect the Virus 题目大意:给定一个编码完的串,将每个字符相应着表的数值转换成6位二进制.然后以8为一个数值,又一次形成字符 串,推断给定询问串是否含有字符 ...

  7. zoj 3430 Detect the Virus(AC自己主动机)

    Detect the Virus Time Limit: 2 Seconds      Memory Limit: 65536 KB One day, Nobita found that his co ...

  8. AC自己主动机

    AC自己主动机 AC自己主动机是KMP和Trie的结合,主要处理多模板串匹配问题.以下推荐一个博客,有助于学习AC自己主动机. NOTONLYSUCCESS  这里另一个Kuangbin开的比赛,大家 ...

  9. POJ 2778 DNA Sequence (AC自己主动机 + dp)

    DNA Sequence 题意:DNA的序列由ACTG四个字母组成,如今给定m个不可行的序列.问随机构成的长度为n的序列中.有多少种序列是可行的(仅仅要包括一个不可行序列便不可行).个数非常大.对10 ...

随机推荐

  1. pm2 start命令中的json格式详解

    pm2 start npm -- start这条命令是pm2的万能命令,pm2 start <json>,就是这一系列命令中的最豪华命令.这个json我们可以理解为一个任务参数描述文件.通 ...

  2. Word转html并移植到web项目

    1.打开对应word文件 建议使用web视图查看文档 这样可以提前预览转转成html样式 2.如果有图片修改图片大小及格式 在web视图下,把图片调制适当大小,不然导出的html可能图片较小 3.点击 ...

  3. bzoj 3555 企鹅QQ

    https://www.lydsy.com/JudgeOnline/problem.php?id=3555 枚举每一位字符,计算字符两侧的哈希值,然后进行比较,用map或排序记录出与其相同的字符串数量 ...

  4. Linux组和提权

    目 录 第1章 组命名管理**    1 1.1 group组信息和密码信息    1 1.1.1 /etc/group 组账户信息    1 1.1.2 /etc/gshadow 组密码信息     ...

  5. 基于tiny4412的u-boot移植(二)(转)

    http://www.cnblogs.com/pengdonglin137/archive/2015/12/27/5080645.html

  6. 昨天去面试,这5个Python面试题都被考到了,Python面试题No6

    第1题:字符串的拼接–如何高效的拼接两个字符串? 字符串拼接的几种方法 加号 逗号 直接连接 格式化 join 多行字符串拼接() 加号 print('Python' + 'Plus') 逗号 pri ...

  7. 【HIHOCODER 1163】 博弈游戏·Nim游戏

    描述 今天我们要认识一对新朋友,Alice与Bob. Alice与Bob总是在进行各种各样的比试,今天他们在玩一个取石子的游戏. 在这个游戏中,Alice和Bob放置了N堆不同的石子,编号1..N,第 ...

  8. 【HDU 5934】Bomb(强连通缩点)

    Problem Description There are N bombs needing exploding. Each bomb has three attributes: exploding r ...

  9. shell实现ftp命令示例

    一.shell脚本示例: [plain] view plaincopy cd /PATH_YOU_WANT_TO_UPLOAD(DOWNLOAD) ftp -niv <<- EOF ope ...

  10. 集合框架学习之List接口

    Java语言的java.util包中提供了一些集合类,这些集合类又被称为容器.用来完善数组的不足之处.集合类与数组的不同之处是,数组的长度是固定的,集合的长度是可变的:数组用来存放基本类型的数据,集合 ...