传送门:http://poj.org/problem?id=3461

Oulipo
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 50450   Accepted: 20018

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 <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int MAXN = 1e6+;
const int MAXW = 1e4+;
char W[MAXW], T[MAXN];
int wlen, tlen;
int nxt[MAXW]; void get_Next()
{
int j = , k = -;
nxt[] = -;
while(j < wlen){
if(k == - || W[j] == W[k]){
nxt[++j] = ++k;
}
else k = nxt[k];
}
} int KMP_count()
{
int ans = , j = ;
if(wlen == && tlen == ){
if(W[] == T[]) return ;
else return ;
}
get_Next();
for(int now = ; now < tlen; now++){
while(j > && T[now] != W[j])
j = nxt[j];
if(W[j] == T[now]) j++;
if(j == wlen){
ans++;
j = nxt[j];
}
}
return ans;
} int main()
{
int T_case;
scanf("%d", &T_case);
while(T_case--){
scanf("%s%s", &W, &T);
wlen = strlen(W);
tlen = strlen(T); printf("%d\n", KMP_count());
}
return ;
}

POJ 3461 Oulipo 【KMP统计子串数】的更多相关文章

  1. POJ 3080 Blue Jeans、POJ 3461 Oulipo——KMP应用

    题目:POJ3080 http://poj.org/problem?id=3080 题意:对于输入的文本串,输出最长的公共子串,如果长度相同,输出字典序最小的. 这题数据量很小,用暴力也是16ms,用 ...

  2. POJ 3461 Oulipo KMP

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

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

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

  4. POJ 3461 Oulipo KMP算法(模板)

    题意: 给两组字符串a和b,求a在b中出现的次数 关于KMP: 马拉车算法是处理回文串,而KMP是处理前后缀的相同字符串的最长长度. a | a | b | a | a | f | a | a 数组 ...

  5. POJ 3461 Oulipo KMP算法题解

    本题就是给出非常多对字符串,然后问一个字符串在另外一个字符串出现的次数. 就是所谓的Strstr函数啦. Leetcode有这道差点儿一模一样的题目. 使用KMP算法加速.算法高手必会的算法了. 另外 ...

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

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

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

    HDU 1686 Oulipo / POJ 3461 Oulipo / SCU 2652 Oulipo (字符串匹配,KMP) Description The French author George ...

  8. POJ 3461 Oulipo(乌力波)

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

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

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

随机推荐

  1. oracle sql命令

    set time on; 设置时间 alter table flashback_test enable row movement; 开启行移 select * from flashback_test ...

  2. 使用Serva通过网络PXE方式安装Windows10/CentOS

    下载Servahttp://www.vercot.com/~serva/download.html也可以从本文附件下载Serva_Community_64_v3.0.0.zip,这是社区版,使用50m ...

  3. C++11并发编程:原子操作atomic

    一:概述 项目中经常用遇到多线程操作共享数据问题,常用的处理方式是对共享数据进行加锁,如果多线程操作共享变量也同样采用这种方式. 为什么要对共享变量加锁或使用原子操作?如两个线程操作同一变量过程中,一 ...

  4. Django自定义登陆验证后台

    支持邮箱/手机号/昵称登录,在django1.6.2测试成功.1.models # -*- encoding: utf-8 -*- from django.db import models from ...

  5. pat00-自测3. 数组元素循环右移问题 (20)

    00-自测3. 数组元素循环右移问题 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 一个数组A中存有N(N>0)个整数,在 ...

  6. SQLAlchemy基本操作和常用技巧

    点击打开链接 Python的ORM框架SQLAlchemy基本操作和常用技巧,包含大量实例,非常好的一个学习SQLAlchemy的教程,需要的朋友可以参考下 python编程语言下的一款开源软件.提供 ...

  7. c#中日期的处理

    DateTime.Now.ToShortDateString()//只取日期DateTime.Now.ToLongTimeString();//只取时间搞定DateTime.Now.ToShortTi ...

  8. 【学习笔记】Java实用类:枚举(Enum)

    Java API(Java Application Programming Interface)Java应用程序编程接口... 实用类:Java API提供了几个常用包: 1.java.lang:编写 ...

  9. Paxos、ZAB、RAFT协议

    这三个都是分布式一致性协议,ZAB基于Paxos修改后用于ZOOKEEPER协议,RAFT协议出现在ZAB协议之后,与ZAB差不多,也有很大区别. 1. Paxos 分布式节点分为3种角色, Prop ...

  10. struts2====之=======初识struts

    ---恢复内容开始--- 1.什么是web框架? 目前应用得较多的三种服务器瑞页面描写技术就是ASP,JSP和PHP.J S P通过在HTMLJî面 文件中嵌入J a v a脚本代码,从而实现动态网页 ...