Oulipo

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 24592    Accepted Submission(s): 9516

Problem Description
The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a member of the Oulipo group. A quote from the book:

Tout avait Pair normal, mais tout s’affirmait faux. Tout avait Fair normal, d’abord, puis surgissait l’inhumain, l’affolant. Il aurait voulu savoir où s’articulait l’association qui l’unissait au roman : stir son tapis, assaillant à tout instant son imagination, l’intuition d’un tabou, la vision d’un mal obscur, d’un quoi vacant, d’un non-dit : la vision, l’avision d’un oubli commandant tout, où s’abolissait la raison : tout avait l’air normal mais…

Perec would probably have scored high (or rather, low) in the following contest. People are asked to write a perhaps even meaningful text on some subject with as few occurrences of a given “word” as possible. Our task is to provide the jury with a program that counts these occurrences, in order to obtain a ranking of the competitors. These competitors often write very long texts with nonsense meaning; a sequence of 500,000 consecutive 'T's is not unusual. And they never use spaces.

So we want to quickly find out how often a word, i.e., a given string, occurs in a text. More formally: given the alphabet {'A', 'B', 'C', …, 'Z'} and two finite strings over that alphabet, a word W and a text T, count the number of occurrences of W in T. All the consecutive characters of W must exactly match consecutive characters of T. Occurrences may overlap.

 
Input
The first line of the input file contains a single number: the number of test cases to follow. Each test case has the following format:

One line with the word W, a string over {'A', 'B', 'C', …, 'Z'}, with 1 ≤ |W| ≤ 10,000 (here |W| denotes the length of the string W).
One line with the text T, a string over {'A', 'B', 'C', …, 'Z'}, with |W| ≤ |T| ≤ 1,000,000.

 
Output
For every test case in the input file, the output should contain a single number, on a single line: the number of occurrences of the word W in the text T.

 
Sample Input
3
BAPC
BAPC
AZA
AZAZAZA
VERDI
AVERDXIVYERDIAN
 
Sample Output
1
3
0
 
  1. //HD1686
  2. #include<iostream>
  3. #include<algorithm>
  4. #include<stdio.h>
  5. #include<string.h>
  6. using namespace std;
  7. const int N = ;
  8. int i, n, m, t, sum;
  9. char T[N], P[N];//P[]是需要比较的字符串,T[]是被比较的字符串
  10. int next1[N];
  11.  
  12. void getnext(char *P, int *next1)
  13. {
  14. int j = ;
  15. int k = -;
  16. next1[] = -;
  17. while (j<m)
  18. {
  19. if (k == - || P[j] == P[k])
  20. {
  21. next1[++j] = ++k;
  22. }
  23. else
  24. {
  25. k = next1[k];
  26. }
  27. }
  28. }
  29. int KMP(char *T, char *P)
  30. {
  31. sum = ;
  32. int i = ;
  33. int j = ;
  34. getnext(P, next1);
  35. while (i<n&&j<m)//注意这里的等号要不要取
  36. {
  37. if (j == - || T[i] == P[j])//匹配的情况,坐标后移
  38. {
  39. i++;
  40. j++;
  41. }
  42. else//如果不匹配
  43. {
  44. j = next1[j];
  45. }
  46. if (j == m)//判断是否匹配结束
  47. {
  48. sum++;
  49. }
  50. }
  51. return sum;
  52. }
  53. int main()
  54. {
  55. scanf("%d", &t);
  56. for (int i = ; i<t; i++)
  57. {
  58. scanf("%s%s", T, P);
  59. m = strlen(P);
  60. n = strlen(T);
  61. printf("%d\n", KMP(T, P));
  62. }
  63. //system("pause");
  64. return ;
  65. }

hdu1686 KMP 求在字符串A中字符串B出现的次数的更多相关文章

  1. Python3求英文文档中每个单词出现的次数并排序

    [本文出自天外归云的博客园] 题目要求: 1.统计英文文档中每个单词出现的次数. 2.统计结果先按次数降序排序,再按单词首字母降序排序. 3.需要考虑大文件的读取. 我的解法如下: import ch ...

  2. C#中如何排除/过滤/清空/删除掉字符串数组中的空字符串

    C#中要如何才能删除一个字符串数组中的空字符串呢?随着微软对C#不断发展和更新,C#中对于数组操作的方式也变得越来越多样化.以往要实现过滤数组中的空字符串,都是需要实行循环的方式来排除和过滤.C#3. ...

  3. python中字符串的操作方法

    python中字符串的操作方法大全 更新时间:2018年06月03日 10:08:51 作者:骏马金龙 我要评论这篇文章主要给大家介绍了关于python中字符串操作方法的相关资料,文中通过示例代码详细 ...

  4. 使用C#删除一个字符串数组中的空字符串

    C#中要如何才能删除一个字符串数组中的空字符串呢?随着微软对C#不断发展和更新,C#中对于数组操作的方式也变得越来越多样化.以往要实现过滤数组中的空字符串,都是需要实行循环的方式来排除和过滤.C#3. ...

  5. hdu 4333"Revolving Digits"(KMP求字符串最小循环节+拓展KMP)

    传送门 题意: 此题意很好理解,便不在此赘述: 题解: 解题思路:KMP求字符串最小循环节+拓展KMP ①首先,根据KMP求字符串最小循环节的算法求出字符串s的最小循环节的长度,记为 k: ②根据拓展 ...

  6. hdu6153 扩展kmp求一个字符串的后缀在另一个字符串出现的次数。

    /** 题目:hdu6153 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6153 题意:给定两个串,求其中一个串t的每个后缀在另一个串s中出现的次数乘以 ...

  7. KMP求字符串最小循环节

    证明1: 对于一个字符串S,长度为L,如果由长度为len的字符串s(字符串s的最小循环节是其本身)循环k次构成,那么字符串s就是字符串S的最小循环节 那么字符串有个很重要的性质和KMP挂钩,即  i ...

  8. hdu 3374 String Problem (字符串最小最大表示 + KMP求循环节)

    Problem - 3374   KMP求循环节. http://www.cnblogs.com/wuyiqi/archive/2012/01/06/2314078.html   循环节推导的证明相当 ...

  9. KMP算法(研究总结,字符串)

    KMP算法(研究总结,字符串) 前段时间学习KMP算法,感觉有些复杂,不过好歹是弄懂啦,简单地记录一下,方便以后自己回忆. 引入 首先我们来看一个例子,现在有两个字符串A和B,问你在A中是否有B,有几 ...

随机推荐

  1. android中finish和system.exit方法退出的区别

    finish只是将此activity推向后台,并没有释放资源. 而system.exit则是杀死进程,会释放资源

  2. Quartz_1_简单编程式任务调度使用(SimpleTrigger)

    最近在工作中,要做定时任务的更能,最开始的时候,想到的是 JavaSE 中,自带 Timer 及 TimerTask 联合使用,完成定时任务.最后发现,随着业务的复杂,JDK 中的 Timer 和 T ...

  3. zabbix启动web界面提示:Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

    问题 zabbix启动web界面提示: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' ( ...

  4. 杭电acm 1021题

    题意是要求能被3整除的数所以为了避免大数据的产生,直接对每个数据求余,然后相加 #include "iostream" using namespace std; int main( ...

  5. GCC 版本与C11标准

    1. GCC版本是否支持C11 C89=C90:gcc选项是:-ansi, -std=c90 or -std=iso9899:; 带了GNU扩展的:-std=gnu90 C94=C95:gcc选项:- ...

  6. vray学习笔记(4)混合材质是个什么东西

    看下定义: The Blend material lets you mix two materials on a single side of the surface. Blend material材 ...

  7. C语言访问网页

    一.理论 http://www.zixue7.com/thread-3860-1-1.html

  8. Luogu 3332 [ZJOI2013]K大数查询

    BZOJ 3110 很早就想写的试炼场题. 不会整体二分啊呜呜呜,只能写写树套树. 有一个trick就是外层使用一个权值线段树,把位置作为下标的线段树放在内层,这样子的话我们在查询$k$大的时候就可以 ...

  9. struts1和struts2之间的区别

    从action类上分析:1.Struts1要求Action类继承一个抽象基类.Struts1的一个普遍问题是使用抽象类编程而不是接口. 2. Struts 2 Action类可以实现一个Action接 ...

  10. hive的not in

    最近项目需要对数据做打平操作,原有的sql使用了not in,但是hive 不支持 not in,晚上搜索了下使用 left outer join select * from lefttbl a le ...