Oulipo
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 46290   Accepted: 18452

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

 
    好久没写字符串都要忘了。。所幸还是花几分钟推出来了= =算是写的比以前简洁了吧。f[i]表示i处失配后转移去的位置。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
#define inf 0x3f3f3f3f
int f[];
char W[];
char T[];
int main()
{
int n,i,j,k,u,v;
int t;
cin>>t;
while(t--){
scanf("%s%s",W,T);
int n1=strlen(W),n2=strlen(T);
f[]=-;
for(i=;i<=n1;++i){
j=f[i-];
while(j!=-&&W[j]!=W[i-]) j=f[j];
f[i]=j+;
}
int ans=;
for(i=,j=;i<n2;++i){
while(j!=-&&W[j]!=T[i]) j=f[j];
if(j==n1-) ans++;
j++;
}
cout<<ans<<endl;
}
return ;
}

poj-3461-kmp模板题。。。的更多相关文章

  1. POJ Oulipo KMP 模板题

    http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4 ...

  2. POJ Oulipo(KMP模板题)

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

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

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

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

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

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

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

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

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

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

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

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

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

  9. poj 3461 Oulipo(KMP模板题)

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

  10. hdu 2544 hdu 1874 poj 2387 Dijkstra 模板题

    hdu 2544  求点1到点n的最短路  无向图 Sample Input2 1 //结点数 边数1 2 3 //u v w3 31 2 52 3 53 1 20 0 Sample Output32 ...

随机推荐

  1. Selenium2.0 Webdriver 随笔

    Webdriver can't action the element when the element is out of view 1. Scroll to the element use Java ...

  2. docker——安装

    Docker划分为CE和EE.CE即社区版(免费,支持后期三个月),EE即企业版,强调安全,付费使用. #安装依赖包 yum install -y yum-utils device-mapper-pe ...

  3. hdu 1569 &1565 (二分图带权最大独立集 - 最小割应用)

    要选出一些点,这些点之间没有相邻边且要求权值之和最大,求这个权值 分析:二分图带权最大独立集. 用最大流最小割定理求解.其建图思路是:将所有格点编号,奇数视作X部,偶数视作Y部,建立源点S和汇点T, ...

  4. 论文笔记:dropout

    Improving neural networks by preventing co-adaptation of feature detectors arXiv preprint arXiv: 120 ...

  5. SpringBoot Web项目中中如何使用Junit

    Junit这种老技术,现在又拿出来说,不为别的,某种程度上来说,更是为了要说明它在项目中的重要性. 凭本人的感觉和经验来说,在项目中完全按标准都写Junit用例覆盖大部分业务代码的,应该不会超过一半. ...

  6. python3_ftp多线程上传图片

    项目中研发人员自己写了ftp服务,没有标准ftp中的列表,准备用jmeter对ftp压力测试时发现jmeter要验证列表(如果有同学用jmeter测试过类似的分享一下方法谢谢了),没办法只能用pyth ...

  7. linux性能分析命令1:top命令

    转载:http://www.cnblogs.com/peida/archive/2012/12/24/2831353.html top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的 ...

  8. Linux数据备份与恢复 dump、restore、dd命令

    dump命令:备份分区.文件或目录 在Linux系统中 dump 命令是没有安装的,所以先安装一下 dump 命令,安装命令如下: [root@localhost -]# yum -y install ...

  9. Redis学习笔记之Redis单机,伪集群,Sentinel主从复制的安装和配置

    0x00 Redis简介 Redis是一款开源的.高性能的键-值存储(key-value store).它常被称作是一款数据结构服务器(data structure server). Redis的键值 ...

  10. mybatis v jpa

    mybatis的优势在于SQL的自由度上,SQL优化和返回对象的大小都是可控的.spring-data-JPA则在开发效率上有优势.