The French author Georges Perec (1936�C1982) 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.

InputThe 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. 
OutputFor 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<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
using namespace std;
#define MAXN 1000001
typedef long long LL;
/*
KMP 查找子串出现的次数
*/
char s[MAXN],t[MAXN];
int Next[MAXN];
void pre_kmp(int m)
{
int j,k;
j = ;k = -;Next[] = -;
while(j<m)
if(k==-||t[k]==t[j])
Next[++j] = ++k;
else
k = Next[k];
}
int KMP(char s[],char t[],int n,int m)
{
int i,j,ans;
i = j = ans = ;
if(n==&&m==)
return (s[]==t[])?:;
pre_kmp(m);
for(i=;i<n;i++)
{
while(j>&&s[i]!=t[j])
j = Next[j];
if(s[i]==t[j])
j++;
if(j==m)
{
ans++;
j = Next[j];
}
}
return ans;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%s%s",t,s);
printf("%d\n",KMP(s,t,strlen(s),strlen(t)));
}
}

B - Oulipo的更多相关文章

  1. C++之路进阶——poj3461(Oulipo)

    Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35694   Accepted: 14424 Descript ...

  2. poj3461 Oulipo(KMP模板)

    Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17795   Accepted: 7160 Descripti ...

  3. Match:Oulipo(POJ 3461)

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

  4. KMP算法 hdu4686 Oulipo

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

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

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

  6. 字符串hash - POJ 3461 Oulipo

    Oulipo Problem's Link ---------------------------------------------------------------------------- M ...

  7. POJ 3461 Oulipo

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

  8. Oulipo (kmp)

    Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26857   Accepted: 10709 Descript ...

  9. POJ 3461 Oulipo(乌力波)

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

  10. A - Oulipo

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

随机推荐

  1. css - 所有的a标签设置为新窗口打开

    前言 由于工作的需要,需要把某个页面下的所有a标签都设置为新开新窗口,即:<a href="XXX">增加target:<a href="XXX&quo ...

  2. codevs1369 xth 砍树(线段树)

    1369 xth 砍树  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond     题目描述 Description 在一个凉爽的夏夜,xth 和 rabbi ...

  3. [Swift通天遁地]五、高级扩展-(8)ImageView(图像视图)的各种扩展方法

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  4. 汇编程序44:检测点13.1 (jmp near ptr 标号指令的中断例程)

    安装程序: assume cs:code //jmp near ptr 标号指令的替代实现,使用iret指令 code segment start: mov ax,cs mov ds,ax mov s ...

  5. Boost.Build特点(译)

    Boost.Build Boost.Build makes it easy to build C++ projects, everywhere. Boost.Build让构建C++项目在任何地方都很容 ...

  6. Android内存管理(8)Dalvik,ART和 .dex 是什么*

    什么是Dalvik: Dalvik是Google公司自己设计用于Android平台的Java虚拟机.Dalvik虚拟机是Google等厂商合作开发的Android移动设备平台的核心组成部分之一. Da ...

  7. 357 Count Numbers with Unique Digits 计算各个位数不同的数字个数

    给定一个非负整数 n,计算各位数字都不同的数字 x 的个数,其中 0 ≤ x < 10n.示例:给定 n = 2,返回 91.(答案应该是除[11,22,33,44,55,66,77,88,99 ...

  8. define与typedef的区别

    define: 发生在预处理阶段,也就是编译之前,仅仅文本替换,不做任何的类型检查 没有作用域的限制 typedef: 多用于简化复杂的类型声明,比如函数指针声明:typedef bool (*fun ...

  9. 使用A*寻路小记

    前几天做另一个DEMO 要用实现自动寻路功能,看到普遍都是A* 学习了下 我的主循环代码: isFindEndPoint = false; //主循环 do { CreateOutSkirtsNode ...

  10. JS高级——闭包练习

    从上篇文章我们知道与浏览器的交互操作如鼠标点击,都会被放入任务队列中,而放入到任务队列中是必须等到主线程的任务都执行完之后才能执行,故而我们有时利用for循环给dom注册事件时候,难以获取for循环中 ...