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

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath> using namespace std; char s1[],s2[];
int next[];
int match[];
int main()
{
int n,i,j,k;
cin>>n;
getchar();
for(i = ;i<n;i++)
{
int num = ;
scanf("%s",s1+);
scanf("%s",s2+);
next[] = ;
int l1 = strlen(s1+);
for(j = ;j<l1+;j++)
{
int t = next[j-];
while(t&&s1[j]!=s1[t+]) t = next[t];
if(s1[j] == s1[t+])t++;
next[j] = t;
}
int l2 = strlen(s2+);
match[] = ;
for(j = ;j<l2+;j++)
{
int t = match[j-];
while(t&&s2[j]!=s1[t+]) t = next[t];
if(s2[j] == s1[t+]) t++;
match[j] = t;
if(match[j] == l1) num++;
}
cout<<num<<endl;
}
}

poj3461Oulipo的更多相关文章

  1. POJ3461Oulipo 题解

    题目大意: 求字符串A在字符串B中出现的次数. 思路: KMP板题,用Hash也可水过~要学习KMP可参考http://blog.csdn.net/u011564456/article/details ...

  2. 【KMP模板】POJ3461-Oulipo

    [题意] 找出第一个字符串在第二个字符串中出现次数. [注意点] 一定要先将strlen存下来,而不能每次用每次求,否则会TLE! #include<iostream> #include& ...

随机推荐

  1. Activity之间通过intent 传递Map

    //传递 Map<String,Object> data=orderlist.get(arg2-1); SerializableMap tmpmap=new SerializableMap ...

  2. .net web api 一

    web api可以提供方便简单可靠的web服务,可以大量的用于不需要提供复杂的soap协议的环境,以简单明了的形式返回数据,在不太复杂的环境中web api可以做为wcf等重级web服务的一种可替代方 ...

  3. SQL SERVER中如何格式化日期(转)

    原文地址:http://blog.sina.com.cn/s/blog_95cfa64601018obo.html   1. SELECT convert(varchar, getdate(), 10 ...

  4. SQL创建/修改数据库、表

    --创建表 create table 表(a1 varchar(10),a2 char(2)) --为表添加描述信息 EXECUTE sp_addextendedproperty N'MS_Descr ...

  5. Mplayer ARM平台下交叉编译

    下载MPlayer http://www.mplayerhq.hu/design7/dload.html 编译环境 系统 : ubuntu 11.04 交叉编译器版本 : Sourcery G++ L ...

  6. 日志管理-将Log4net的配置配置到的独立文件中

    转自:http://www.cnblogs.com/zfanlong1314/p/3662679.html使用log4net已经很久了.但从来没有详情了解log4的参数,及具体使用方法.看了周公的博客 ...

  7. struts2框架的核心内容

     Struts1和Struts2的区别和对比: Action 类: • Struts1要求Action类继承一个抽象基类.Struts1的一个普遍问题是使用抽象类编程而不是接口,而struts2的Ac ...

  8. Plugin is too old, please update to a more recent version, or set ANDROID_DAILY_OVERRIDE environment variable to “*****”

    Plugin is too old, please update to a more recent version, or set ANDROID_DAILY_OVERRIDE environment ...

  9. php 支持递归函数.递归函数就是调用函数本身.

    例子 将一个字符进行颠倒 function reverse_r($str){ if(strlen($str)){ reverse_r(substr($str,1));// } echo substr( ...

  10. Thinking in Java——集合(Collection)

    一.ArrayList的使用(略) 二.容器的基本概念 (一).Collection是集合类的基本接口 主要方法: public interface Collection<E>{ bool ...