http://poj.org/problem?id=3461

Oulipo
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 41051   Accepted: 16547

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
KMP模板题
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
using namespace std;
typedef long long ll;
int nex[];
char s[],p[];
void get_next(char *s)
{
nex[]=-;
int i=,k=-;
int len=strlen(s);
while(i<len)
{
if(k==- || p[i]==p[k])
{
i++;
k++;
nex[i]=k;
}
else k=nex[k];
}
}
void kmp(char *s,char *p)
{
int ans=;
get_next(s);
int len=strlen(p);
int i=,j=;
while(i<len)
{
if(j==- || p[i]==s[j])
{
i++;
j++;
}
else j=nex[j];
if(j==strlen(s)) ans++;
}
printf("%d\n",ans);
}
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
scanf("%s%s",s,p);
kmp(s,p);
}
return ;
}
 

POJ Oulipo KMP 模板题的更多相关文章

  1. POJ Oulipo(KMP模板题)

    题意:找出模板在文本串中出现的次数 思路:KMP模板题 #include<cstdio> #include<cstring> #include<cmath> #in ...

  2. HDU 1686 - Oulipo - [KMP模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 Time Limit: 3000/1000 MS (Java/Others) Memory Li ...

  3. poj3461 Oulipo (KMP模板题~) 前面哪些也是模板题 O.O

    # include <stdio.h> # include <algorithm> # include <string.h> using namespace std ...

  4. HDU 1711 - Number Sequence - [KMP模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Time Limit: 10000/5000 MS (Java/Others) Memory L ...

  5. POJ:3461-Oulipo(KMP模板题)

    原题传送:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS Memory Limit: 65536K Description The F ...

  6. Number Sequence - HDU 1711(KMP模板题)

    题意:给你一个a串和一个b串,问b串是否是a串的子串,如果是返回b在a中最早出现的位置,否则输出-1   分析:应该是最简单的模板题了吧..... 代码如下: ==================== ...

  7. hdu 1711 Number Sequence(KMP模板题)

    我的第一道KMP. 把两个数列分别当成KMP算法中的模式串和目标串,这道题就变成了一个KMP算法模板题. #include<stdio.h> #include<string.h> ...

  8. HDU 1711Number Sequence【KMP模板题】

    <题目链接> 题目大意: 意思是给出两个串,找出匹配串在模式串中的位置. 解题分析: KMP算法模板题. #include <cstdio> #include <cstr ...

  9. (模板)poj3461(kmp模板题)

    题目链接:https://vjudge.net/problem/POJ-3461 题意:给出主串和模式串,求出模式串在主串中出现的次数. 思路:kmp板子题. AC代码: #include<cs ...

随机推荐

  1. Google笔试(2015年8月)

    华电北风吹 天津大学认知计算与应用重点实验室 日期:2015/8/21 这三道题目的PDF能够在这里下载 https://github.com/ncepuzhengyi/jobHuntingExam/ ...

  2. mysql-总结select各子句及其顺序

    顺序:from->where ->group by->having ->order by

  3. android openGL ES2 一切从绘制纹理開始

    纹理.在openGL中,能够理解为载入到显卡显存中的图片.Android设备在2.2開始支持openGL ES2.0.从前都是ES1.0 和 ES1.1的版本号.简单来说,openGL ES是为了嵌入 ...

  4. WebAPI的自动化监控和预警

    Metrics.net + influxdb + grafana 构建WebAPI的自动化监控和预警 前言 这次主要分享通过Metrics.net + influxdb + grafana 构建Web ...

  5. rest_framework-认证-总结完结篇

    执行过程 APIView() Ruquest() Authentication() OrderView()APIView() def duspatch: self.initial(request) d ...

  6. IT男送什么礼物给女朋友呢?

    每到各种节日,加班到吐血的IT男们,总是没什么时间准备礼物给女朋友,偶尔想起又不知道送什么的好,女朋友虽然心里不说,多少有些难过.现在推荐朋友的羊毛毡手工店,每一件都是亲手制作,特别是可以个性定制,女 ...

  7. IHttpHandler的学习(0)

    本片文章转自网络 问题1:什么是HttpHandler?(Handler:处理者:那就是对Http请求的处理拉) 问题2:什么是HttpModule? 问题3:什么时候应该使用HttpHandler什 ...

  8. ACM训练联盟周赛(第三场)

    A.Teemo's bad day Today is a bad day. Teemo is scolded badly by his teacher because he didn't do his ...

  9. 用@property (copy) NSMutableArray *array;会有什么问题?

    我们县回忆一下copy属性的作用,一个属性标记了copy,当你调用他的setter方法,他会建立一个索引计数为1的对象,然后释放旧对象. @property (copy) NSMutableArray ...

  10. linux安装memcacehed

        1.wget http://www.danga.com/memcached/dist/memcached-1.2.5.tar.gz     2.wget http://www.monkey.o ...