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

Source

【分析】
模板
 /*
宋代李之仪
卜算子·我住长江头
我住长江头,君住长江尾。日日思君不见君,共饮长江水。
此水几时休,此恨何时已。只愿君心似我心,定不负相思意。
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <iomanip>
#include <string>
#include <cmath>
#include <queue>
#include <assert.h>
#include <map>
#include <ctime>
#include <cstdlib>
#include <stack>
#define LOCAL
const int MAXN = + ;
const int INF = ;
const int SIZE = ;
const int MAXM = + ;
const int maxnode = 0x7fffffff + ;
using namespace std;
int l1, l2;
char a[MAXN], b[MAXN];
int next[];//不用开太大了..
void getNext(){
//初始化next数组
next[] = ;
int j = ;
for (int i = ; i <= l1; i++){
while (j > && a[j + ] != a[i]) j = next[j];
if (a[j + ] == a[i]) j++;
next[i] = j;
}
return;
}
int kmp(){
int j = , cnt = ;
for (int i = ; i <= l2; i++){
while (j > && a[j + ] != b[i]) j = next[j];
if (a[j + ] == b[i]) j++;
if (j == l1){
cnt++;
j = next[j];//回到上一个匹配点
}
}
return cnt;
} void init(){
scanf("%s", a + );
scanf("%s", b + );
l1 = strlen(a + );
l2 = strlen(b + );
} int main(){
int T; scanf("%d", &T);
while (T--){
init();
getNext();
printf("%d\n", kmp());
}
/*scanf("%s", a + 1);
l1 = strlen(a + 1);
getNext();
for (int i = 1; i <= l1; i++) printf("%d ", next[i]);*/
return ;
}

【POJ3461】【KMP】Oulipo的更多相关文章

  1. 【KMP】【最小表示法】NCPC 2014 H clock pictures

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1794 题目大意: 两个无刻度的钟面,每个上面有N根针(N<=200000),每个 ...

  2. 【动态规划】【KMP】HDU 5763 Another Meaning

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 题目大意: T组数据,给两个字符串s1,s2(len<=100000),s2可以被解读成 ...

  3. 【洛谷】3375 KMP字符串匹配

    [算法]KMP [题解][算法]字符串 #include<cstdio> #include<algorithm> #include<cstring> using n ...

  4. HDOJ 2203 亲和串 【KMP】

    HDOJ 2203 亲和串 [KMP] Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...

  5. 【kmp】bzoj3620: 似乎在梦中见过的样子

    考察kmp理解题 Description “Madoka,不要相信 QB!”伴随着 Homura 的失望地喊叫,Madoka 与 QB 签订了契约. 这是 Modoka 的一个噩梦,也同时是上个轮回中 ...

  6. 【KMP】Censoring

    [KMP]Censoring 题目描述 Farmer John has purchased a subscription to Good Hooveskeeping magazine for his ...

  7. 【KMP】OKR-Periods of Words

    [KMP]OKR-Periods of Words 题目描述 串是有限个小写字符的序列,特别的,一个空序列也可以是一个串.一个串P是串A的前缀,当且仅当存在串B,使得A=PB.如果P≠A并且P不是一个 ...

  8. 【KMP】Radio Transmission

    问题 L: [KMP]Radio Transmission 题目描述 给你一个字符串,它是由某个字符串不断自我连接形成的.但是这个字符串是不确定的,现在只想知道它的最短长度是多少. 输入 第一行给出字 ...

  9. 【kmp】似乎在梦中见过的样子

    参考博客: BZOJ 3620: 似乎在梦中见过的样子 [KMP]似乎在梦中见过的样子 题目描述 「Madoka,不要相信QB!」伴随着Homura的失望地喊叫,Madoka与QB签订了契约. 这是M ...

  10. P5410 【模板】扩展 KMP

    P5410 [模板]扩展 KMP #include<bits/stdc++.h> using namespace std; ; int q, nxt[maxn], extend[maxn] ...

随机推荐

  1. Perfect Squares——Leetcode

    Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...

  2. GCC的编译和安装 很好的资料

    http://blog.csdn.net/yrj/article/details/492404 1.GCC的编译和安装2.预处理    #define 可以支持不定数量的参数.    例子如下:    ...

  3. 用redis实现支持优先级的消息队列

    http://www.cnblogs.com/tianqiq/p/4309791.html http://www.cnblogs.com/it-cen/p/4312098.html http://ww ...

  4. ios新特性

    @import  在xcode 5 下,为了更易于开发,增加了modules和 auto-linking 这两个新特性: 在以前,如果你要使用MapKit这个框架,你要这样做 1) 使用语句 #imp ...

  5. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(27)-权限管理系统-分配用户给角色

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(27)-权限管理系统-分配用户给角色 分配用户给角色,跟分配角色给用户操作是基本一致的. 打开模块维护,展 ...

  6. 谈一下怎样设计Oracle 分区表

    在谈设计Oracle分区表之间先区分一下分区表和表空间的个概念: 表空间:表空间是一个或多个数据文件的集合,全部数据对象都存放在指定的表空间中,但主要存放表,故称表空间. 分区表:分区致力于解决支持极 ...

  7. JAVA Calendar具体解释

    (在文章的最后,将会介绍Date类,假设有兴趣,能够直接翻到最后去阅读) 到底什么是一个 Calendar 呢?中文的翻译就是日历,那我们立马能够想到我们生活中有阳(公)历.阴(农)历之分.它们的差别 ...

  8. Linux kernel驱动相关抽象概念及其实现 之“linux设备模型kobject,kset,ktype”

    kobject,kset,ktype三个很重要的概念贯穿Linux内核驱动架构,特转载一篇博文: (转载自http://blog.csdn.net/gdt_a20/article/details/64 ...

  9. Lucene IndexReader,IndexWriter,IndexSearcher 缓存应用

    1.IndexManager类,用于提供IndexReader,IndexWriter,IndexSearcher获取接口 import java.io.File; import java.io.IO ...

  10. [置顶] iptables 性能 测试

    一直研究iptables 性能,这几天刚好有硬件资源,于是发始下手测试iptables NAT 性…… 硬件环境  : 服务器: IBM x3650 ( 4G  E5645 6核 12线程) ESXi ...