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. django的路由系统

    在django生成的工程项目文件中urls.py文件用于指定路由信息 该文件默认导入以下模块 from confimport from import admin from confimport url ...

  2. css基础之 font的简写规则 以及 自定义 CSS3 @font-face详细用法

    Part 1 font简写 CSS的命名规则是用英文字母 数字 和下划线(一般用小写)来命名.简写css font的好处有三:一是写起来方便(就像键盘快捷键):二是简化代码:三是帮助你熟悉和深刻理解c ...

  3. poj 01背包

    首先我是按这篇文章来确定题目的. poj3624 Charm Bracelet 模板题 没有要求填满,所以初始化为0就行 #include<cstdio> #include<algo ...

  4. PHP读取CSV大文件导入数据库的示例

    对于数百万条数据量的CSV文件,文件大小可能达到数百M,如果简单读取的话很可能出现超时或者卡死的现象. 为了成功将CSV文件里的数据导入数据库,分批处理是非常必要的. 下面这个函数是读取CSV文件中指 ...

  5. 网络受限下,使用Nexus要解决的两个问题

    在网络受限的情况下,使用nexus总会遇到这么两个问题,让你头疼. 我头疼过了,为了不让大家头疼,把解决方案放在这里,供大家参考.   问题一.背景: 由于网络原因,Nexus无法更新远程仓库的索引. ...

  6. ORA-04092: COMMIT 不能在触发器中

    触发器无需commit也不能写commit触发器和触发它的DML是同一个事务DML提交了,触发器的操作也提交了,要不就一起回滚了 当然,如果你一定要在触发器里写COMMIT那就用自治事务相当于一个事务 ...

  7. 【python学习笔记02】python的数据类型2

    列表和元组之间的主要区别是:列表括在括号([])和它们的元素和大小是可以改变的,而元组在圆括号(),不能被更新.元组可以被认为是只读列表. 存储在一个列表中的值可以使用切片操作符来访问([]和[:]) ...

  8. ASCII码排序,hdu-2000

    Problem Description 输入三个字符后,按各字符的ASCII码从小到大的顺序输出这三个字符.   Input 输入数据有多组,每组占一行,有三个字符组成,之间无空格.   Output ...

  9. Sql Server专题三:SQL操作与技巧

    一.基础 1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sql server --- ...

  10. 如何测试一个U盘

    功能测试: 1 在windows xp比较流行的操作系统上是否可以识别(装了驱动后是否可以) 2 在电脑上显示的盘符是否正确 3 总空间,可用空间,已用空间是否显示正确 4 u盘中是否可以拷入各种格式 ...