Oulipo
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 49378   Accepted: 19617

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

C/C++:

 #include <map>
#include <queue>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <climits>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
const int my_max = 1e6 + ; int my_len1, my_len2, my_next[my_max];
char my_str1[my_max], my_str2[my_max]; int my_kmp()
{
int my_cnt = ;
memset(my_next, , sizeof(my_next));
for (int i = , j = ; i < my_len1; ++ i)
{
while (j > && my_str1[i] != my_str1[j]) j = my_next[j];
if (my_str1[i] == my_str1[j]) ++ j;
my_next[i + ] = j;
}
for (int i = , j = ; i < my_len2; ++ i)
{
while (j > && my_str1[j] != my_str2[i]) j = my_next[j];
if (my_str1[j] == my_str2[i]) ++ j;
if (j == my_len1) my_cnt ++, j = my_next[j];
}
return my_cnt;
} int main()
{
int t;
scanf("%d", &t);
while (t --)
{
scanf("%s", my_str1);
my_len1 = strlen(my_str1);
scanf("%s", my_str2);
my_len2 = strlen(my_str2); printf("%d\n", my_kmp());
}
return ;
}

poj 3461 Oulipo(KMP)的更多相关文章

  1. poj 3461 Oulipo(kmp统计子串出现次数)

    题意:统计子串出现在主串中的次数 思路:典型kmp #include<iostream> #include<stdio.h> #include<string.h> ...

  2. poj 3461 Oulipo(KMP模板题)

    Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 36903   Accepted: 14898 Descript ...

  3. POJ 3461 Oulipo(——KMP算法)

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

  4. POJ 3461 Oulipo(KMP裸题)

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

  5. POJ 题目3461 Oulipo(KMP)

    Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26479   Accepted: 10550 Descript ...

  6. POJ 3461 Oulipo(乌力波)

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

  7. HDU 1686 Oulipo , 同 POJ 3461 Oulipo (字符串匹配,KMP)

    HDU题目 POJ题目 求目标串s中包含多少个模式串p KMP算法,必须好好利用next数组,, (kmp解析)——可参考 海子的博客  KMP算法 //写法一: #include<string ...

  8. POJ 3461 Oulipo[附KMP算法详细流程讲解]

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

  9. POJ 3461 Oulipo 【KMP统计子串数】

    传送门:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submission ...

随机推荐

  1. PMP(第六版)中的各种矩阵表格

  2. 移动端前端常见的触摸相关事件touch、tap、swipe

    前端的很多事件在PC端和浏览器端可公用,但有些事件却只在移动端产生,如触摸相关的事件 本文整理了移动端常见的一些事件,包括原生支持的click.touch.tap.swipe事件,也有定义型的gest ...

  3. 【Spring Cloud】实战项目搭建

    0.项目搭建 创建maven项目,删除其中的src目录,作为整体父项目,在其中添加module实现各个组件. 1.Eureka Server的实现 添加module,创建Spring Boot项目,添 ...

  4. google::Glog

    windows下使用google的Glog库 下载glog-.tar.gz,解压. vs2013打开工程, 有四个项目 libglog libglog_static logging_unittest ...

  5. hadoop2.x的安装

    可以自己从官网编译打包也可以直接下载官网的.gz包.自己编译打包的过程如下: .查看是否安装cmake.svn.openssl.ncurses,没有的直接安装上 yum list|grep cmake ...

  6. spring在IoC容器中装配Bean详解

    1.Spring配置概述 1.1.概述 Spring容器从xml配置.java注解.spring注解中读取bean配置信息,形成bean定义注册表: 根据bean定义注册表实例化bean: 将bean ...

  7. Redux的核心概念,实现代码与应用示例

    Redux是一种JavaScript的状态管理容器,是一个独立的状态管理库,可配合其它框架使用,比如React.引入Redux主要为了使JavaScript中数据管理的方便,易追踪,避免在大型的Jav ...

  8. jvm虚拟机栈的作用

    jvm虚拟机栈的作用 jvm虚拟机栈栈帧的组成 jvm虚拟机栈,也叫java栈,它由一个个的栈帧组成,而栈帖由以下几个部分组成 局部变量表-存储方法参数,内部使用的变量 操作数栈-在变量进行存储时,需 ...

  9. Asp.Net Core 单元测试正确姿势

    背景 ASP.NET Core 支持依赖关系注入 (DI) 软件设计模式,并且默认注入了很多服务,具体可以参考 官方文档, 相信只要使用过依赖注入框架的同学,都会对此有不同深入的理解,在此无需赘言. ...

  10. NPOI 导出 excel 性能测试

    NPOI 导出 excel 性能测试 Intro 网上看到很多人说 NPOI 的性能不行,自己写了一个 NPOI 的扩展库,于是想尝试看看 NPOI 的性能究竟怎么样,道听途说始终不如自己动手一试. ...