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
题意描述:
输入两个串s1和s2
计算并输出s1在s2中能够匹配多少次
解题思路:
KMP模板题,加深对next数组的理解,主要问题是更新i或者j的位置,其实不用更新,你会发现其实到最后j是会自动返回的。
代码实现:
 #include<stdio.h>
#include<string.h>
char s[],t[];
void get_next(char t[],int next[],int l2);
int kmp(char s[],char t[]);
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%s%s",t,s);
printf("%d\n",kmp(s,t));
}
return ;
}
int kmp(char s[],char t[])
{
int i,j,l1,l2,c; int next[];//next[]数组中存的是左上匹配串前后缀的相似度,从0到l2
l1=strlen(s);
l2=strlen(t);
get_next(t,next,l2); c=;
i=;
j=;
while(i < l1)
{
if(j==- || s[i] == t[j])
{
i++;
j++;
}
else//少了个else
j=next[j];
if(j==l2)//i和j均不用改动
c++;
}
return c;
}
void get_next(char t[],int next[],int l2)
{
int i,j;
i=;
j=-;
next[]=-;
while(i < l2)
{
if(j==- || t[i] == t[j])
{
i++;
j++;
if(t[i] != t[j])
next[i]=j;
else
next[i]=next[j];
}
else//少了个else
j=next[j];
}
/*for(i=0;i<=l2;i++)
printf("%d ",next[i]);
printf("\n");*/
}

易错分析:

1、next数组存储的结果是错位的,正好能够被下次利用。

2、代码能力,注意细节,像少个else,找了半天错。

POJ 3461 Oulipo(——KMP算法)的更多相关文章

  1. [POJ] 3461 Oulipo [KMP算法]

    Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23667   Accepted: 9492 Descripti ...

  2. POJ 3461 Oulipo KMP算法题解

    本题就是给出非常多对字符串,然后问一个字符串在另外一个字符串出现的次数. 就是所谓的Strstr函数啦. Leetcode有这道差点儿一模一样的题目. 使用KMP算法加速.算法高手必会的算法了. 另外 ...

  3. POJ 3461 Oulipo KMP算法(模板)

    题意: 给两组字符串a和b,求a在b中出现的次数 关于KMP: 马拉车算法是处理回文串,而KMP是处理前后缀的相同字符串的最长长度. a | a | b | a | a | f | a | a 数组 ...

  4. POJ 3461 Oulipo KMP

    题意:统计其中一个子串的出现次数 题解:即KMP算法中j==m的次数 //作者:1085422276 #include <cstdio> #include <cmath> #i ...

  5. POJ 3080 Blue Jeans、POJ 3461 Oulipo——KMP应用

    题目:POJ3080 http://poj.org/problem?id=3080 题意:对于输入的文本串,输出最长的公共子串,如果长度相同,输出字典序最小的. 这题数据量很小,用暴力也是16ms,用 ...

  6. POJ 3461 Oulipo(KMP,模式串在主串中出现次数 可重叠)

    题意:给你两个字符串p和s,求出p在s中出现的次数. 显然,我们要先把模式串放到前面,之后主串放后面,中间隔开,这样就可以根据前缀数组的性质来求了. 我先想直接把p接到s前面,之后求Next数组对st ...

  7. 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 ...

  8. POJ 3461 Oulipo(乌力波)

    POJ 3461 Oulipo(乌力波) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] The French autho ...

  9. POJ 3461 Oulipo[附KMP算法详细流程讲解]

      E - Oulipo Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

  10. POJ 3461 Oulipo(字符串匹配,KMP算法)

    题意:给出几组数据,每组有字符串W和T,问你W在T中出现几次. 思路:字符串长度很大,用KMP算法. 一开始写的是:调用KMP算法查找W在T中是否匹配,若匹配,则个数+1.则接下来T的索引移动相应的距 ...

随机推荐

  1. JMeter集合点

    位置:添加--> 定时器-->Synchronizing Timer     注意:集合点放在所有操作之前.   假设线程组线程数设置的是50个,那么希望50个都准备好一块上,那么集合点中 ...

  2. CSS 去掉inline-block间隙的几种方法

    最近做移动端页面时,经常会用到inline-block元素来布局,但无可避免都会遇到一个问题,就是inline-block元素之间的间隙.这些间隙会导致一些布局上的问题,需要把间隙去掉.对于inlin ...

  3. Vuejs之开发环境搭建

    Vue.js Vue.js是目前很火的一个前端框架,采用MVVM模式设计,它是以数据驱动和组件化的思想构建的.相比于Angular.js,Vue.js提供了更加简洁.更易于理解的API,使得我们能够快 ...

  4. java基础之关键字static

        在java当中有很多关键字,static便是其中一个,它很普通但我们经常需要运用到它,所以要了解static是非常有必要的.   鉴于本人知识结构有限,若有错误忘不吝赐教,甚为感谢.   一. ...

  5. H5 + 开发App(分享功能)

    我们开发App有一个不可少的功能,就是分享功能.让用户将app分享到他的社交圈.比如微信 QQ 微博等等. 准备工作:我们要先去申请相关的权限, 这是传送门http://ask.dcloud.net. ...

  6. 实战开发-》融云tp3.2.3

    1.先去下载sdk 2.我放在的位置如下: 3.之前试了试,怎么引入都不成功,所以我加入了命名空间 还有,我把methods下的所有类都加了命名空间,心里安慰吧. 4.在公共函数写函数,例如 填写自己 ...

  7. windows错误处理

    在调用windows API时函数会首先对我们传入的参数进行校验,然后执行,如果出现什么情况导致函数执行出错,有的函数可以通过返回值来判断函数是否出错,比如对于返回句柄的函数如果返回NULL 或者IN ...

  8. 有关求任意一个正整数的n的因数的个数的求解思路

    已知条件:n=p1^a1xp2^a2xp3^a3........xpk^ak;求解n的因数的个数: 求解的主要思想:递归 设所有的因数的个数为U1: 则U1会等于什么呢? 不妨设求得p2^a2xp3^ ...

  9. python开发_python中str.format()

    格式化一个字符串的输出结果,我们在很多地方都可以看到,如:c/c++中都有见过 下面看看python中的字符串格式函数str.format(): 1 #使用str.format()函数 2 3 #使用 ...

  10. [转载]mysql中实现行号,oracle中的rowid

    mysql中实现行号需要用到MYSQL的变量,因为MySql木有rownumber. MYSQL中变量定义可以用 set @var=0 或 set @var:=0 可以用=或:=都可以,但是如果变量用 ...