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

KMP板子题

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm> using namespace std;
void kmp_pre(char x[],int m,int next[]) {
int i,j;
j=next[0]=-1;
i=0;
while(i<m) {
while(-1!=j && x[i]!=x[j])j=next[j];
next[++i]=++j;
}
}
int next[1000010];
int KMP_Count(char x[],int m,char y[],int n) { //x 是模式串,y 是主串
int i,j;
int ans=0;
//preKMP(x,m,next);
kmp_pre(x,m,next);
i=j=0;
while(i<n) {
while(-1!=j && y[i]!=x[j])j=next[j];
i++;
j++;
if(j>=m) {
ans++;
j=next[j];
}
}
return ans;
} char a[10005];
char b[1000005];
int main() { int T;
cin>>T;
while(T--) {
scanf("%s",a);
scanf("%s",b);
printf("%d\n",KMP_Count(a,strlen(a),b,strlen(b)));
} return 0;
}

Oulipo (KMP出现次数)的更多相关文章

  1. hdu 1686 Oulipo KMP匹配次数统计

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 分析:典型的KMP算法,统计字符串匹配的次数. 用Next数组压缩时间复杂度,要做一些修改. / ...

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

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

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

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

  4. HDU 1686 Oulipo (KMP 可重叠)

    题目链接 Problem Description The French author Georges Perec (1936–1982) once wrote a book, La dispariti ...

  5. poj3461 Oulipo(KMP模板)

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

  6. Oulipo (kmp)

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

  7. POJ 3461 Oulipo KMP

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

  8. POJ3461–Oulipo(KMP)

    题目大意 给定一个文本串和模式串,求模式串在文本串中出现的次数 题解 正宗KMP 代码: #include<iostream> #include<cstring> #inclu ...

  9. hdu Oulipo(kmp)

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

随机推荐

  1. resin容器更改JDK

    更改resin的jdk版本,找到resin的配置文件:Resin\contrib\init.resin文件,找到 JAVA_HOME=@JAVA_HOME@ RESIN_HOME=@resin_hom ...

  2. Android中SQLite查询date类型字段出现有返回但是为错误值的情况

    出现该情况的原因是因为查询精度与数据库中存储精度不相同造成的,例如,查询精度为 YYYY-MM-DD 但是存储精度为 YYYY-MM-DD HH:MM:SS,就会出现该错误. 更改查询精度为YYYY- ...

  3. Camera 3D概念

    1. integration time即积分时间是以行为单位表示曝光时间(exposure time)的,比如说INT TIM为159,就是指sensor曝光时间为159行,两者所代表的意思是相同的, ...

  4. NSWindow上添加NSView

    DBSCustomView *view = [[DBSCustomView alloc] initWithFrame:NSMakeRect(100, 100, 100, 100)]; [self.wi ...

  5. Entity Framework Tutorial Basics(10):Entity Lifecycle

    Entity Lifecycle: Before we work on CRUD operation (Create, Read, Update, Delete), it's important to ...

  6. Makefile的使用

    Makefile 使用 一.实验说明 课程说明 在先前的课程中,我们已经学习了 gcc 和 gdb 的使用.本节课程中,我们将介绍 Makefile 的使用.Makefile带来的好处就是--&quo ...

  7. WPARAM和LPARAM的含义

    lParam 和 wParam 是宏定义,一般在消息函数中带这两个类型的参数,通常用来存储窗口消息的参数. LRESULT CALLBACK WindowProc(HWND hwnd, UINT uM ...

  8. [转]Marshaling a SAFEARRAY of Managed Structures by P/Invoke Part 3.

    1. Introduction. 1.1 In part 1 of this series of articles, I demonstrated how to transfer managed ar ...

  9. Java NIO学习笔记

    Java NIO学习笔记 一 基本概念 IO 是主存和外部设备 ( 硬盘.终端和网络等 ) 拷贝数据的过程. IO 是操作系统的底层功能实现,底层通过 I/O 指令进行完成. 所有语言运行时系统提供执 ...

  10. gRPC官方文档(gRPC基础:C++)

    文章来自gRPC 官方文档中文版 本教程提供了C++程序员如何使用gRPC的指南. 通过学习教程中例子,你可以学会如何: 在一个 .proto 文件内定义服务. 用 protocol buffer 编 ...