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. [原创] Assistant editor 取消拖拽 binding 的 UI 元素

    1. press up-right button "show the utilities" 2. press "show the Connections inspecto ...

  2. js 函数(function)

    <Javascript高级程序设计第三版> 3.7 函数 1. ECMAScript中的函数在定义时,不必指定是否返回值. 2. 位于return语句之后的任何code都永远不会执行.(之 ...

  3. MySQL数据库主从同步安装与配置总结

    MySQL的主从同步是一个很成熟的架构,优点为: ①在从服务器可以执行查询工作(即我们常说的读功能),降低主服务器压力: ②在从主服务器进行备份,避免备份期间影响主服务器服务: ③当主服务器出现问题时 ...

  4. VS2003转VS2010 fatal error C1189: #error

    我自己的mfc的demo要转换编译环境出现以下编译错误: VS2010编译错误:fatal error C1189: #error : This file requires _WIN32_WINNT ...

  5. Python 在Windows下安装matplotlib

    windows下安装很麻烦,使用easy_install 安装报错  提示缺少freetype 和png 后经多方查询,最终安装成功 以下是安装过程 前提你的Python环境已经搭建好了 1.前提需要 ...

  6. 进程外Session和进程内Session存储

  7. MYSQL 查看可用的字符集的 2 方法

    方法 1. show character set; 方法 2. show collation;

  8. Nginx 配置指令的执行顺序(三)

    如前文所述,除非像 ngx_set_misc 模块那样使用特殊技术,其他模块的配置指令即使是在 rewrite 阶段运行,也不能和 ngx_rewrite 模块的指令混合使用.不妨来看几个这样的例子. ...

  9. Nginx 变量漫谈(一)

    Nginx 的配置文件使用的就是一门微型的编程语言,许多真实世界里的 Nginx 配置文件其实就是一个一个的小程序.当然,是不是“图灵完全的”暂且不论,至少据我观察,它在设计上受 Perl 和 Bou ...

  10. 评价早期SaaS创业公司时,投资人在关注什么?(是否有机会发展成一个平台,长期的护城河)

    编者按: 当聊到早期项目时,人们经常会问投资人一个问题:“在评价早期 SaaS 创业公司时,投资人会关注什么——指标还是其他方面?” Nakul Mandan 作为 Lightspeed 风投机构的合 ...