Oulipo

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

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. #include <iostream>
  2. #include<cstdio>
  3. #include<string>
  4. #include<cstring>
  5. int next[], num;
  6. char a[], b[];
  7. int lena, lenb;
  8. void get_next()//自身和自身比较,找某种特征
  9. {
  10. int i, j;
  11. i = ; next[] = -; j = -;
  12. while (i<lenb)
  13. {
  14. if (j == - || b[i] == b[j])//匹配成功的话,继续下一位
  15. {
  16. i++;
  17. j++;
  18. if (b[i] == b[j])
  19. {
  20. next[i] = next[j];
  21. }
  22. else
  23. {
  24. next[i] = j;
  25. }
  26. }
  27. else//否则返回到可以匹配的位置
  28. {
  29. j = next[j];
  30. }
  31. }
  32. }
  33. void index_KMP()
  34. {
  35. int i, j;
  36. i = ;
  37. j = ;
  38. num = ;
  39. while (i<lena)
  40. {
  41. if (a[i] == b[j] || j == -) //进行两个字符串的匹配
  42. {
  43. i++;//匹配成功,往后继续匹配
  44. j++;
  45. }
  46. else//匹配完成一次,代表出现了一次,记录下来
  47. {
  48. j = next[j];
  49. }
  50. if (j == lenb)//匹配失败,寻找数组中可以匹配的位置
  51. {
  52. num++;
  53. j = next[j];
  54. }
  55. }
  56. printf("%d\n", num);
  57. }
  58. int main()
  59. {
  60. int n;
  61. while (scanf("%d", &n) != EOF)
  62. {
  63. getchar();
  64. while (n--)
  65. {
  66. gets(b);
  67. gets(a);
  68. lena = strlen(a);
  69. lenb = strlen(b);
  70. get_next();
  71. index_KMP();
  72. }
  73. }
  74. return ;
  75. }
 

HDU 1686 Oulipo(优化的KMP)的更多相关文章

  1. 题解报告:hdu 1686 Oulipo(裸KMP)

    Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, w ...

  2. HDU 1686 Oulipo / POJ 3461 Oulipo / SCU 2652 Oulipo (字符串匹配,KMP)

    HDU 1686 Oulipo / POJ 3461 Oulipo / SCU 2652 Oulipo (字符串匹配,KMP) Description The French author George ...

  3. HDU - 1686 Oulipo KMP匹配运用

    id=25191" target="_blank" style="color:blue; text-decoration:none">HDU - ...

  4. hdu 1686 Oulipo KMP匹配次数统计

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 分析:典型的KMP算法,统计字符串匹配的次数. 用Next数组压缩时间复杂度,要做一些修改. / ...

  5. HDU 1686 - Oulipo - [KMP模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 Time Limit: 3000/1000 MS (Java/Others) Memory Li ...

  6. hdu 1686 Oulipo kmp算法

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1686 题目: Problem Description The French author George ...

  7. HDU 1686 Oulipo(KMP变形求子串出现数目(可重))

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 题目大意:给两个字符串A,B求出A中出现了几次B(计算重复部分). 解题思路:稍微对kmp()函 ...

  8. hdu 1686 Oulipo (kmp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 题目大意:寻找子链在母链中出现的次数. #include <iostream> #i ...

  9. HDU 1686 Oulipo , 同 POJ 3461 Oulipo (字符串匹配,KMP)

    HDU题目 POJ题目 求目标串s中包含多少个模式串p KMP算法,必须好好利用next数组,, (kmp解析)——可参考 海子的博客  KMP算法 //写法一: #include<string ...

随机推荐

  1. cf 429 B Working out

    B. Working out time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  2. Python学习札记(三十二) 面向对象编程 Object Oriented Program 3

    参考:访问限制 NOTE 1.eg. #!/usr/bin/env python3 class Student(object): """docstring for Stu ...

  3. [翻译]将智能指针用于C++的类成员

    http://stackoverflow.com/questions/15648844/using-smart-pointers-for-class-members Question: I'm hav ...

  4. [osg][原创]osg多屏幕显示,会出现透明需要设置的问题

    同事由于新加了一个屏幕,本来用 osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer(); viewer->s ...

  5. idea 设置编译快捷键(代替 ctrl+f9)

    问题描述 今日在设置项目热部署的时候,无奈就是不会自动编译,不知什么原因. 而编译的话,要么去点小按钮 ,要么使用ctrl + f9,实在不便. 且ctrl + f9目测不能更改. 解决办法 借鉴了关 ...

  6. selenium 3.6.0 geckodriver的一次坑

    Traceback (most recent call last):  File "./se3.py", line 16, in <module>    dr=webd ...

  7. C# DataTable Compute方法的使用

    在开发中需要对DataTable的数据进行处理,比如累加,求最大最小及平均值等,以前都采用手工对DataTable进行循环并计算的方式,现在发现DataTable的Compute方法可以轻松实现这些功 ...

  8. bzoj2705: [SDOI2012]Longge的问题 欧拉定理

    题意:给定一个整数N,你需要求出∑gcd(i, N)(1<=i <=N). 题解:考虑n的所有因子,假设有因子k,那么对答案的贡献gcd(i,n)==k的个数即gcd(i/k,n/k)== ...

  9. [转]Linux下彻底卸载mysql详解

    http://www.jb51.net/article/97516.htm 一.使用以下命令查看当前安装mysql情况,查找以前是否装有mysql 1 rpm -qa|grep -i mysql 可以 ...

  10. IEnumerable<T> 接口和GetEnumerator 详解

    IEnumerable<T> 接口 .NET Framework 4.6 and 4.5   公开枚举数,该枚举数支持在指定类型的集合上进行简单迭代. 若要浏览此类型的.NET Frame ...