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

题意:看样例。
KMP模板,程序模拟+手动模拟大概是明白了一些。
 #include <iostream>
#include <cstdio>
#include <cstring> using namespace std; char A[+],B[+];
int T[+];
int n,ans; void calc_T()//找到失配函数
{
T[]=-;
int j,lenA=strlen(A);
for (int i=;i<lenA;i++)
{
j=T[i];
while (j!=- && A[j]!=A[i]) j=T[j];
T[i+]=++j;
}
} void kmp(int lenA)
{
calc_T();
int j=,k=,lenB=strlen(B);
while (k<lenB && j<lenA)
{
if (k==- || A[j]==B[k]) j++,k++;
else k=T[k];
if (k==lenB) ans++,k=T[k];
}
//return ans;
} int main()
{
while (~scanf("%d",&n))
{
for (int i=;i<=n;i++)
{
ans=;
scanf("%s%s",B,A);
kmp(strlen(A));
printf("%d\n",ans);
}
}
}

Code

【POJ3461】Olipo的更多相关文章

  1. 【POJ3461】【KMP】Oulipo

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

  2. 【poj3461】 Oulipo

    http://poj.org/problem?id=3461 (题目链接) 题意 求一个字符串在另一个字符串中出现的次数. Solution KMP裸题,太久没写过了,都忘记怎么求next数组了..水 ...

  3. 【POJ3461】Oulipo

    题面 The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter ...

  4. Python高手之路【六】python基础之字符串格式化

    Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...

  5. 【原】谈谈对Objective-C中代理模式的误解

    [原]谈谈对Objective-C中代理模式的误解 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 这篇文章主要是对代理模式和委托模式进行了对比,个人认为Objective ...

  6. 【原】FMDB源码阅读(三)

    [原]FMDB源码阅读(三) 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 FMDB比较优秀的地方就在于对多线程的处理.所以这一篇主要是研究FMDB的多线程处理的实现.而 ...

  7. 【原】Android热更新开源项目Tinker源码解析系列之一:Dex热更新

    [原]Android热更新开源项目Tinker源码解析系列之一:Dex热更新 Tinker是微信的第一个开源项目,主要用于安卓应用bug的热修复和功能的迭代. Tinker github地址:http ...

  8. 【调侃】IOC前世今生

    前些天,参与了公司内部小组的一次技术交流,主要是针对<IOC与AOP>,本着学而时习之的态度及积极分享的精神,我就结合一个小故事来初浅地剖析一下我眼中的“IOC前世今生”,以方便初学者能更 ...

  9. Python高手之路【三】python基础之函数

    基本数据类型补充: set 是一个无序且不重复的元素集合 class set(object): """ set() -> new empty set object ...

随机推荐

  1. sdut 2441 屠夫与狼

    屠夫和狼 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 题目链接:http://acm.sdut.edu.cn/sdutoj/p ...

  2. Asyncio中的Task管理

    #!/usr/bin/env python # -*- coding: utf-8 -*- import asyncio import datetime import time from random ...

  3. phpcms-v9系统搭建wap网站及单页面

    如需要绑定域名为wap.domain.com,作下如操作: 一.把wap.domain.com域名绑定到你的这个网站主机上. 二.在网站后台——模块——手机门户域名里面填写“http://wap.do ...

  4. 搭建Mantis 缺陷管理系统(转)

    转自 什么是Mantis MantisBT is a free popular web-based bugtracking system (feature list). It is written i ...

  5. WINDOWS系统Eclipse+NDK+Android + OpenCv

    WINDOWS系统Eclipse+NDK+Android + OpenCv 参考文档博客 1 NDK环境搭建 http://jingyan.baidu.com/article/5d6edee22d90 ...

  6. LayoutInflater(一)

    相信接触Android久一点的朋友对于LayoutInflater一定不会陌生,都会知道它主要是用于加载布局的.而刚接触Android的朋友可能对LayoutInflater不怎么熟悉,因为加载布局的 ...

  7. OpenGL大作业

    GLfloat light0_position[] = { 15.0,15.0,15.0,10.0 };//定义光源位置 103glLightfv(GL_LIGHT0, GL_POSITION, li ...

  8. supervisor(一)基础篇

    这两天干的活,是让楼主写一个supervisor的listener,用来监控supervisor所管理子进程的状态,当子进程异常退出时,楼主写的这个listener将会触发报警.在这里总结下super ...

  9. SQL多表查询案例

    表结构: emp表: dept表: salgrade表: (1)查出至少有一个员工的部门.显示部门编号.部门名称.部门位置.部门人数. SELECT z.*,d.dname,d.loc FROM de ...

  10. js 判断数组的值是否有重复

    方法一: var s = ary.join(",")+","; for(var i=0;i<ary.length;i++) { if(s.replace( ...