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

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. c++中&amp;和&amp;&amp;有什么差别

    他们不同点在于&&相当一个开关语句,就是说假设&&前面值为false那么他就不继续运行后面的表达式:而&无论前面的值为什么,总是运行其后面的语句. &能 ...

  2. 通信网Project之——单源单宿最短路问题

    最主要的Vertex类: #ifndef VERTEX_H #define VERTEX_H #include <climits> #include <cstddef> #de ...

  3. 异常处理 Access to the path is denied

    在需要进行读写操作的目录下添加Network Service这个帐号, 由于在iis 6.0中,默认的应用程序池中的标示用的是Network Service, 所以在进程中是使用Network Ser ...

  4. android网络操作使用汇总(http)

    Android是作为智能手机的操作系统,我们开发的应用,大多数也都须要连接网络,通过网络发送数据.获取数据,因此作为一个应用开发人员必须熟悉怎么进行网络訪问与连接. 通常android中进行网络连接通 ...

  5. Ubuntu 14.04 编译newLISP 10.6.0

    1. 确保安装了gcc4.8.2 2. 安装须要的库: apt-get install libreadline6 libreadline6-dev 3. 下载并解压newLISP源码,这个不多说了,去 ...

  6. java设计模式之五原型模式(Prototype)

    原型模式虽然是创建型的模式,但是与工程模式没有关系,从名字即可看出,该模式的思想就是将一个对象作为原型,对其进行复制.克隆,产生一个和原对象类似的新对象.本小结会通过对象的复制,进行讲解.在Java中 ...

  7. SQL 无限级分类语句

    原文:SQL 无限级分类语句 原表数据为: 此处用到了with关键字,在程序中也可以用递归实现,但觉得还是没有一条sql方便 with tb (ID,Name,ParentID,Sort) as( s ...

  8. CSS3制作

    目标是制作如下面DEMO显示的一个日历效果: HTML Markup 先来看看其结构: <div class="calendar"> <span class=&q ...

  9. python基础课程_学习笔记20:标准库:有些收藏夹——os

    标准库:有些收藏夹 os os模块为您提供访问多个操作系统服务特征. os和它的子模块os.path还包含一些用于检查.构造.删除文件夹和文件的函数,以及一些处理路径的函数. os模块中一些重要函数和 ...

  10. Repository在DDD中的应用

    Repository在DDD中的应用2014-10-09 08:55 by Jesse Liu, 98 阅读, 0 评论, 收藏, 编辑 概述 上一篇我们算是粗略的介绍了一下DDD,我们提到了实体.值 ...