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. leveldb源码分析之Slice

    转自:http://luodw.cc/2015/10/15/leveldb-02/ leveldb和redis这样的优秀开源框架都没有使用C++自带的字符串string,redis自己写了个sds,l ...

  2. csp-s模拟95

    近两场开始文件评测,感觉和平时真不太一样. 考试时间是多了$10min$,但处理文件.输入输出什么的杂事也是忙来忙去.这可能也最像真实的$csps$了,稍有疏忽可能都是致命伤. T1.T2挂的对拍全打 ...

  3. pwn学习日记Day18 《程序员的自我修养》读书笔记

    知识杂项 obj文件:当前源代码编译成二进制目标文件 exe文件:将.obj文件与库文件.lib等文件链接生成的可执行文件 一个现代编译器的主要工作流程如下: 源程序(source code)→ 预处 ...

  4. 重读APUE(15)-pthread_cond_wait与while循环

    即使pthead_cond_wait()和pthread_cond_timewait()没有错误返回,等待的条件也可能是假的:即使pthread_cond_timewait()返回了超时错误,关联的条 ...

  5. qt 创建及调用QT的 DLL

    先讲一下对QT动态链接库的调用方法,主要包括: 1.显式链接DLL,调用DLL的全局函数,采用Qt的QLibrary方法 2.显示链接DLL,调用DLL中类对象.成员函数.(通过对象即可实现类成员函数 ...

  6. 验证HashSet和HashMap不是线程安全

    JAVA集合类: java.util包下的HashSet和HashMap类不是线程安全的, java.util.concurrent包下的ConcurrentHashMap类是线程安全的. 写2个测试 ...

  7. 20190710记录:去掉中转图,直接以1280*1024进行反坐标计算,填补pbFinal。

    1.记录:去掉中转图,直接以1280*1024进行反坐标计算.pbFinal=1280*1024. // Imagejoint.cpp : 定义控制台应用程序的入口点. // #include &qu ...

  8. 阿里云服务器怎么用ip访问不了

    个人想做个网站,就买了个阿里云服务器,不得不说阿里的安全做的很好,但是对于我这种小白,就成了麻烦了 工具/原料   阿里云服务器 方法/步骤     首先百度搜索阿里云,进入登录界面,输入账号密码登录 ...

  9. CockroachDB学习笔记——[译]The New Stack:遇见CockroachDB,一个弹性SQL数据库

    原文链接:https://www.cockroachlabs.com/blog/the-new-stack-meet-cockroachdb-the-resilient-sql-database/ 原 ...

  10. windows10环境下pip安装Scrapy报错

    问题描述 当前环境win10,python_3.6.1,64位. 在windows下,在dos中运行pip install Scrapy报错: building 'twisted.test.raise ...