题目意思:找到上串在下串中有多少个

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
#include<stdio.h>
#include<string.h> int next[10005],lenw,lent;
char W[10005],T[1000005]; void set_next()
{
int i=0,j=-1;
next[0]=-1;
lenw=strlen(W);
while(i<lenw)
{
if(j==-1||W[i]==W[j])
{
i++;j++;
next[i]=j;
}
else
j=next[j];
}
}
int KMP()
{
int k=0,i=0,j=0;
lent=strlen(T);
while(i<lent)
{
if(T[i]==W[j]||j==-1)
{
i++;j++;
}
else
j=next[j];
if(j==lenw)
{
k++; j=next[j];//表示W的第j个以前都与T的从i-next[j]到第i-1这段已经匹好了
} //next[j]的值是在W[j]前面的串中最长前缀和最长后缀相等,那么
//当前的匹配成功,就说明W的第j个以前都与T的从i-next[j]到第i-1这段也已经匹配好了
}
return k;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
getchar();
scanf("%s %s",W,T);
set_next();
printf("%d\n",KMP());
}
}

hdu1686的更多相关文章

  1. 哈希算法解决:HDU1686 && POJ2774 && POJ3261

    HDU1686 题意: 找A串在B串中的出现次数(可重叠),可用KMP做,这里只提供哈希算法做的方法 题解: 先得到A串的hash值,然后在B中枚举起点,长度为lena的子串,检验hash值是否相同就 ...

  2. hdu----1686 Oulipo (ac自动机)

    Oulipo Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  3. hdu1686 KMP

    简单KMP 求单词出现的次数.直接可以考虑,在每一次匹配成功时,让ans++,k=next[k],直到结束. #include<stdio.h> #include<string.h& ...

  4. HDU-1686 Oulipo

    学习:重点理解这句话的意思: next[j]会告诉我们从哪里开始匹配     模板题. Oulipo Time Limit: 3000/1000 MS (Java/Others)    Memory ...

  5. HDU1686——Oulipo

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

  6. HDU1686:Oulipo

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

  7. hdu-1686(kmp)

    题意:前面的都是废话...其实直接看输入要求和输出要求就可以了,就是给你两个字符串,问你第一个字符串在第二个字符串中出现几次: 解题思路:kmp... 代码: #include<iostream ...

  8. hdu1686 Oulipo KMP/AC自动机

    The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e ...

  9. hdu1686字符串kmp

    The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e ...

随机推荐

  1. 私人定制javascript中函数小知识点

    函数的定义 首先在javascript中,函数就是对象,程序可以随意操控它们.比如,可以给它们设置属性,甚至调用它们的方法.函数使用function关键字来定义.它既可以用在函数定义表达式,也可以用在 ...

  2. 2013级C++第13周(春)项目——继承的进一步话题与GUI应用开发

    课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接 第一部分 程序阅读:阅读以下类的定义,请说出在 ...

  3. [CLR via C#]1.5 本地代码生成器:NGen.exe

    原文:[CLR via C#]1.5 本地代码生成器:NGen.exe 1. NGen.exe工具,可以在一个程序安装到用户计算机时,将IL代码编译成为本地代码.由于代码在安装时已经编译好,所以CLR ...

  4. oracle 电子商务解决方案讲义

    1. 电商营销(CRM) - 高端客户体验 2. 当当网李国庆做 "千千面"购物体验 3. 所使用的唯一的产品oracle的CRM 4. 个人的事情.谁在世界上是用户体验. 5. ...

  5. JSR303 Bean Validation 技术规范特性概述

    概述 Bean Validation 规范 Bean 是 Java Bean 的缩写,在 Java 分层架构的实际应用中,从表示层到持久化层,每一层都需要对 Java Bean 进行业务符合性验证,如 ...

  6. 在windows下用C语言写socket通讯实例

    原文:在windows下用C语言写socket通讯实例 From:Microsoft Dev Center #undef UNICODE #define WIN32_LEAN_AND_MEAN #in ...

  7. sp.Net MVC4 + Oracle + EasyUI + Bootstrap2

    Asp.Net MVC4 + Oracle + EasyUI + Bootstrap 第二章   Asp.Net MVC4 + Oracle + EasyUI + Bootstrap 第二章 --使用 ...

  8. select刷新后,保持选定状态,Cookies存储select选定状态信息

    //cookies存储select选定值,防止刷新后没了 window.onload = function () { var cooki = document.cookie; if (cooki != ...

  9. 讲故事的人写的谈判手册——Leo锦书64

    正如其名称所暗示这本书"谈判无处不在".从决定谈判的成功或失败的因素一个不同的观点,测量中详细给出的同一时间. 图书出版不错,这是阅读的样车.阅读收获压力较小的方式.   书能给读 ...

  10. TextArea中定位光标位置

    原文:TextArea中定位光标位置 在项目中,遇到一个场景:希望能在TextArea中输入某条记录中的明细(明细较简单,没有附属信息,只用记录顺序和值即可,譬如用"+"号来作为明 ...