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

题意:

给出T组数据,每组数据有两个字符串,让我们查找第一个字符串在第二个字符串中出现的次数,可重叠;

思路:kmp;

注意:使用C++中cin、cout作为输入输出语句将导致超时,也是怪坑的

具体代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define maxx 2000100
char a[maxx],b[];
int n,m;
int p[maxx];
void pre()
{
int i=,j=-;
p[]=-;
while(i<m)
{
if(j==-||b[i]==b[j])
{
j++,i++;
p[i]=j;
}
else
j=p[j];
}
}
void kmp()
{
int i=,j=,ans=;
while(i<n)
{
if(j==-||a[i]==b[j])
{
i++,j++;
if(j==m)
{
ans++;
j=p[j];
}
}
else
j=p[j];
}
printf("%d\n",ans);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%s%s",b,a);
n=strlen(a);//
m=strlen(b);
pre();
kmp();
}
return ;
}

下面来一道求不重叠部分的题目

剪花布条 HDU - 2087(kmp,求不重叠匹配个数)

Oulipo POJ - 3461(kmp,求重叠匹配个数)的更多相关文章

  1. 剪花布条 HDU - 2087(kmp,求不重叠匹配个数)

    Problem Description 一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案.对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢? Input 输入 ...

  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. POJ 3978 Primes(求范围素数个数)

    POJ 3978 Primes(求范围素数个数) id=3978">http://poj.org/problem? id=3978 题意: 给你一个区间范围A和B,要你求出[A,B]内 ...

  4. (KMP)Oulipo -- poj --3461

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=92486#problem/B http://poj.org/problem?id=3461 ...

  5. Match:Oulipo(POJ 3461)

     Oulipo 题目大意:给你一个字符串,要你找到字符串包含指定子串的个数 只要你知道了KMP,这一题简直不要太简单,注意STL的string是会超时的,还是乖乖用char吧 #include < ...

  6. POJ 3461 kmp 应用

    题意:求匹配串在文本中出现次数,KMP应用,理解了就OK了,每次匹配成功就累加次数,开始的时候超时, 由于在处理每次成功的时候让i=i-len2+1,相当于回溯了,后来一想,本次成功,相当于" ...

  7. AC日记——Oulipo poj 3461

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37958   Accepted: 15282 Description The ...

  8. POJ 3461 kmp

    Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40168   Accepted: 16135 Descript ...

  9. POJ 2752 (kmp求所有公共前后缀长度)

    <题目链接> <转载于> 题目大意:  给出一个字符串str,求出str中存在多少子串,使得这些子串既是str的前缀,又是str的后缀.从小到大依次输出这些子串的长度.即输出该 ...

随机推荐

  1. while练习

    题目:企业发放的奖金根据利润提成.利润(I)低于或等于10万元时,奖金可提10%:利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%:20万到40万之 ...

  2. Echarts案例-柱状图

    一:先在官网下载 https://www.echartsjs.com/zh/download.html 然后再建立工程,导入这两个包: 写代码: <!DOCTYPE html> <h ...

  3. JavaWeb_(SpringMVC框架)测试SpringMVC&Spring&MyBatis三大整合

    搭建 SpringMVC&Spring&MyBatis三大整合 传送门 1.准备 测试搭建S pringMVC&Spring&MyBatis三大整合 用例   a)准备 ...

  4. Js 之将字符串当变量使用

    var page1 = 0; var p = "page1"; //修改值 window[p] += 1; var value = eval(p);

  5. Mac 卸载Python3.6

    Mac 自带的 Python 已经能够满足我们的需要了,因此很多同学在安装完 Python 之后,又想要将其删除,或者称之为卸载. 对于删除 Python,我们首先要知道其具体都安装了什么,实际上,在 ...

  6. QMessageBox改变大小

    创建一个QMessageBox: QMessageBox msgBox(this);msgBox.setWindowTitle(tr("MailBox Location"));ms ...

  7. ubuntu下安装g++

    主要来自ubuntu中文社区http://www.ubuntu.org.cn/support/documentation/doc/VMware 首选,确认你已经安装了build-essential程序 ...

  8. MISS YOU

      文章来源:刘俊涛的博客 欢迎关注,有问题一起学习欢迎留言.评论

  9. 二、navicat连接本地数据库以及远程数据库

    本地连接 1.打开navicat 2.连接 最后点击确定就连接成功了: 远程数据库 和上面一样.....

  10. centos6.5安装mysql(转载,亲测可用)

    如果要在Linux上做j2ee开发,首先得搭建好j2ee的开发环境,包括了jdk.tomcat.eclipse的安装(这个在之前的一篇随笔中已经有详细讲解了Linux学习之CentOS(七)--Cen ...