Oulipo
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 26479   Accepted: 10550

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

field=source&key=BAPC+2006+Qualification" style="text-decoration:none">BAPC 2006 Qualification

ac代码

<pre name="code" class="cpp">#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char s1[100100],s2[1000100];
int next[1000100];
void getnext(char *s)
{
int len=strlen(s);
int i,j;
i=0;j=-1;
next[0]=-1;
while(i<=len)
{
if(j==-1||s[i]==s[j])
{
i++;
j++;
next[i]=j;
}
else
j=next[j];
}
}
int kmp(char *s1,char *s2)
{
int len1=strlen(s1);
int len2=strlen(s2);
memset(next,0,sizeof(next));
getnext(s1);
int i,j,ans;
i=j=ans=0;
while(j<=len2)
{
if(i==-1||s1[i]==s2[j])
{
i++;
j++;
}
else
i=next[i];
if(i==len1)
{
ans++;
i=next[i];
}
}
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%s",s1);
scanf("%s",s2);
printf("%d\n",kmp(s1,s2));
}
}



POJ 题目3461 Oulipo(KMP)的更多相关文章

  1. POJ 3461 Oulipo KMP算法题解

    本题就是给出非常多对字符串,然后问一个字符串在另外一个字符串出现的次数. 就是所谓的Strstr函数啦. Leetcode有这道差点儿一模一样的题目. 使用KMP算法加速.算法高手必会的算法了. 另外 ...

  2. POJ 3080 Blue Jeans、POJ 3461 Oulipo——KMP应用

    题目:POJ3080 http://poj.org/problem?id=3080 题意:对于输入的文本串,输出最长的公共子串,如果长度相同,输出字典序最小的. 这题数据量很小,用暴力也是16ms,用 ...

  3. [POJ] 3461 Oulipo [KMP算法]

    Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23667   Accepted: 9492 Descripti ...

  4. POJ 3461 Oulipo KMP

    题意:统计其中一个子串的出现次数 题解:即KMP算法中j==m的次数 //作者:1085422276 #include <cstdio> #include <cmath> #i ...

  5. POJ 3461 Oulipo KMP算法(模板)

    题意: 给两组字符串a和b,求a在b中出现的次数 关于KMP: 马拉车算法是处理回文串,而KMP是处理前后缀的相同字符串的最长长度. a | a | b | a | a | f | a | a 数组 ...

  6. POJ 3461 Oulipo(KMP,模式串在主串中出现次数 可重叠)

    题意:给你两个字符串p和s,求出p在s中出现的次数. 显然,我们要先把模式串放到前面,之后主串放后面,中间隔开,这样就可以根据前缀数组的性质来求了. 我先想直接把p接到s前面,之后求Next数组对st ...

  7. POJ——T 3461 Oulipo

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

  8. HDU 1686 Oulipo / POJ 3461 Oulipo / SCU 2652 Oulipo (字符串匹配,KMP)

    HDU 1686 Oulipo / POJ 3461 Oulipo / SCU 2652 Oulipo (字符串匹配,KMP) Description The French author George ...

  9. POJ 3461 Oulipo 【KMP统计子串数】

    传送门:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submission ...

随机推荐

  1. NVL和NVL2有什么区别,NULLIF 的使用.

    NULL指的是空值,或者非法值. NVL (expr1, expr2):expr1为NULL,返回expr2:不为NULL,返回expr1.注意两者的类型要一致 NVL2 (expr1, expr2, ...

  2. SQL server存储过程学习

    由于之前使用 Linq to Sql来操作数据库,对于数据库的存储过程.函数等比较薄弱.乘着自己闲着的时候,就百度自学了一点存储过程,以防以后要用. 基础通俗易懂的存储过程通过 存储过程学习 ,然后自 ...

  3. [OpenWrt]安装mjpg-streamer

    安装mjpg-streamer 远程监控基本上是wifi小车的一个必备功能了.摄像头我用的是奥尼百脑通 D881,这个要100左右. 确认安装了以下软件: kmod-usb2 kmod-video-u ...

  4. 【Oracle】回收站

    ☆回收站概念 oracle从10g开始,引入回收站(Recycle Bin)概念.回收站的全称叫:Tablespace Recycle Bin.回收站是一个逻辑区域,oracle并没有为它分配物理空间 ...

  5. 根据业务自己设计的.NET工厂模式架构

    最近项目的架构需要做调整优化,根据业务需要写了一个简单的工厂模式架构 项目介绍:整个系统分为三大平台(这里用A,B,C来标示),每个平台又细分为多个APP客户端(每个APP都有appid来区分) 因为 ...

  6. caffe学习笔记--跑个SampleCode

    Caffe默认情况会安装在CAFFERROOT,就是解压到那个目录,例如: home/username/caffe-master, 所以下面的工作,默认已经切换到了该工作目录.下面的工作主要是,用于测 ...

  7. 服务端Linux机器日志查看命令

    常用查看命令 ps -ef | grep java 查看当前机器运行程序: tail -100f /... 查看日志,实时的 less /- 按页查看日志,空格翻页,q退出 cat /.. | gre ...

  8. 2018秋招blibli算法工程师

    我给出代码如下:和之前做数塔(dp的入门题目)的思路一致 dp[i][j]为走到坐标(i,j)的最小减速(只有向右走和向上走两种情况) #include<stdio.h> #include ...

  9. 【转】Oracle基础结构认知——oracle内存结构 礼记八目 2017-12-15 20:31:27

    oracle的数据库实例是一组后台进程和内存结构组成的,而内存结构由系统全局区(system global area)和程序全局区(program global area)组成. #修改SGA和PGA ...

  10. VMware WorkStation 用 VMTools 官方下载地址

    每次安装 VMTools 都不成功,谷歌到了这个地址,特地分享. 先打开这个网址, 选择你的 VMware WorkStation 对应的版本号: http://softwareupdate.vmwa ...