Oulipo
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 35694   Accepted: 14424

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经典例题,因为题目说可压缩,所以末尾+1的失败指针指向1,1的失败指针指向0,就ok了。

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define maxn 1000000 using namespace std; int next[maxn],n;
char W[maxn],T[maxn]; void getnext()
{
int j=,k=-;next[]=-;
while (!j||W[j]!=)
{
if (k==-||W[j]==W[k])
{
j++,k++;
if (W[j]!=W[k]) next[j]=k;else next[j]=next[k];
}else k=next[k];
}
} int kmp()
{
int i=,j=,num=;
memset(next,,sizeof(next));
int T_len=strlen(T),W_len=strlen(W);
getnext();
while (i<T_len)
{
if (j==-||T[i]==W[j]) i++,j++;
else j=next[j];
if (j==W_len)
{
num++;
j=next[j];
}
}
return num;
} int main()
{
scanf("%d",&n);
for (int i=;i<=n;i++)
{
scanf("%s%s",W,T);
printf("%d\n",kmp());
}
return ;
}

C++之路进阶——poj3461(Oulipo)的更多相关文章

  1. C++之路进阶codevs1269(匈牙利游戏)

    1269 匈牙利游戏 2012年CCC加拿大高中生信息学奥赛  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond       题目描述 Description ...

  2. poj3461 Oulipo(KMP模板)

    Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17795   Accepted: 7160 Descripti ...

  3. C++之路进阶——优先队列优化最短路径算法(dijkstra)

    一般的dijkstra算法利用贪心的思想,每次找出最短边,然后优化到其他点的的距离,我们还采用贪心思路,但在寻找最短边进行优化,之前是双重for循环,现在我们用优先队列来实现. 代码解释: //样例程 ...

  4. poj3461 Oulipo

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

  5. KMP——POJ-3461 Oulipo && POJ-2752 Seek the Name, Seek the Fame && POJ-2406 Power Strings && POJ—1961 Period

    首先先讲一下KMP算法作用: KMP就是来求在给出的一串字符(我们把它放在str字符数组里面)中求另外一个比str数组短的字符数组(我们叫它为ptr)在str中的出现位置或者是次数 这个出现的次数是可 ...

  6. C++之路进阶——HDU1880(魔咒词典)

    ---恢复内容开始--- New~ 欢迎参加2016多校联合训练的同学们~ 魔咒词典 Time Limit: 8000/5000 MS (Java/Others)    Memory Limit: 3 ...

  7. C++之路进阶——P2022

    P2022 有趣的数 让我们来考虑1到N的正整数集合.让我们把集合中的元素按照字典序排列,例如当N=11时,其顺序应该为:1,10,11,2,3,4,5,6,7,8,9. 定义K在N个数中的位置为Q( ...

  8. C++之路进阶codevs1242(布局)

    1242 布局 2005年USACO  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold    <:section class="hbox" ...

  9. C++之路进阶——codevs3333(高级打字机)

    3333 高级打字机  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 大师 Master     题目描述 Description 早苗入手了最新的高级打字机.最新款自然有着与 ...

随机推荐

  1. Win7下完全卸载Oracle 11g

    1 右击“计算机”-->管理-->服务和应用程序-->服务,停掉所有Oracle相关的服务(以Oracle打头的,比如OracleDBConsoleorcl). 2 开始--> ...

  2. 【PostgreSQL】PostGreSQL数据库,时间数据类型

    ---"17:10:13.236"time without time zone:时:分:秒.毫秒 ---"17:10:13.236+08"time with t ...

  3. TEST===>Sqlserver中获取年月日时分秒

    可以用两种方法获取 1. select GETDATE() as '当前日期', DateName(year,GetDate()) as '年', DateName(month,GetDate()) ...

  4. Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:1.7:run (dist) on project hadoop-kms: An Ant BuildException has occured

    编译cdh版hadoop2.5.0出现的问题 系统: CentOs66 64位 JDK:1.7 Maven: 3.0.5 Protobuf: libprotoc 2.5.0 编译命令: mvn pac ...

  5. 13.final关键字

    1.final修饰的变量只能赋一次值,不赋值时,会提示初始化 2.final修饰的方法不能被重写 3.final修饰的类不能被继承

  6. Javascript的shift()和push(),unshift()和pop()方法简介

    栈方法: Javascript为数组专门提供了push()和pop()方法,以便实现类似栈的行为.来看下面的例子: var colors=new Array();       //创建一个数组 var ...

  7. bzoj 2058+2059+2060 Usaco2010 Nov

    三道金组比较容易的题目.. 2058 首先交换次数就是逆序对数,因为只能交换相邻的两数 先对原序列找逆序对数 用树状数组nlogn求出 然后O(n)依次求出其循环序列的逆序对数 比如 3 5 4 2 ...

  8. PAT (Basic Level) Practise 1040 有几个PAT(DP)

    1040. 有几个PAT(25) 时间限制 120 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CAO, Peng 字符串APPAPT中包含了两个单 ...

  9. Java数据校验(Bean Validation / JSR303)

    文档: http://beanvalidation.org/1.1/spec/ API : http://docs.jboss.org/hibernate/beanvalidation/spec/1. ...

  10. js 时间函数封装

    html代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...