Oulipo

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 7200    Accepted Submission(s): 2867

Problem 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
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  1358 3336 3746 3068 2222 

KMP模板题。注意k的赋值,题目要求是包括几个 不是能切割几个。如asdasd      asdasdasd   这样的情况下结果应该是2  所以当k==plen后再计算,k的赋值不能从0再開始,而是从pre[k]開始。

#include <stdio.h>
#include <string.h>
#include <algorithm>
#define maxn 1000000+10 using namespace std; int pre[maxn];
char T[maxn];
char P[10010]; void getpre(int plen)
{
pre[0]=pre[1]=0;
for(int i=1;i<plen;i++){
int k=pre[i];
while(k&&P[i]!=P[k])k=pre[k];
pre[i+1]=P[i]==P[k]? k+1:0;
}
} int search(int slen,int plen)
{
int num=0;
getpre(plen);
int k=0;
for(int i=0;i<slen;i++){
while(k&&P[k]!=T[i])k=pre[k];
if(P[k]==T[i])k++;
if(k==plen){
++num;
k=pre[k]; //从0開始是挪动了plen。题目是找出包括几个,不是能切割几个。所以从pre[k]開始。
}
}
return num;
} int main()
{
int t;
scanf("%d",&t);
while(t--){
memset(pre,0,sizeof(pre));
scanf("%s%s",P,T);
int slen=strlen(T);
int plen=strlen(P);
int res=search(slen,plen);
printf("%d\n",res);
}
return 0;
}

HDOJ Oulipo 1686【KMP】的更多相关文章

  1. HDOJ 2203 亲和串 【KMP】

    HDOJ 2203 亲和串 [KMP] Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...

  2. 【KMP】【最小表示法】NCPC 2014 H clock pictures

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1794 题目大意: 两个无刻度的钟面,每个上面有N根针(N<=200000),每个 ...

  3. 【动态规划】【KMP】HDU 5763 Another Meaning

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 题目大意: T组数据,给两个字符串s1,s2(len<=100000),s2可以被解读成 ...

  4. HDOJ 1501 Zipper 【DP】【DFS+剪枝】

    HDOJ 1501 Zipper [DP][DFS+剪枝] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...

  5. 【KMP】Censoring

    [KMP]Censoring 题目描述 Farmer John has purchased a subscription to Good Hooveskeeping magazine for his ...

  6. 【KMP】OKR-Periods of Words

    [KMP]OKR-Periods of Words 题目描述 串是有限个小写字符的序列,特别的,一个空序列也可以是一个串.一个串P是串A的前缀,当且仅当存在串B,使得A=PB.如果P≠A并且P不是一个 ...

  7. 【KMP】Radio Transmission

    问题 L: [KMP]Radio Transmission 题目描述 给你一个字符串,它是由某个字符串不断自我连接形成的.但是这个字符串是不确定的,现在只想知道它的最短长度是多少. 输入 第一行给出字 ...

  8. 【kmp】似乎在梦中见过的样子

    参考博客: BZOJ 3620: 似乎在梦中见过的样子 [KMP]似乎在梦中见过的样子 题目描述 「Madoka,不要相信QB!」伴随着Homura的失望地喊叫,Madoka与QB签订了契约. 这是M ...

  9. 【POJ3461】【KMP】Oulipo

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

随机推荐

  1. splay模板整理

    1.插入一个数 void insert(int x) { if (!root) { ++tot; e[tot].left = e[tot].right = e[tot].fa = ; e[tot].v ...

  2. 【WC2019笔记】IOI2018 / ACM题目选讲

    哇!济南的 rqy 大佬讲课!就是 $luogu$ 上有名的那位! 上面这句话写错了,请大家无视 XylophoneIOI2018 练习赛 T2题意:交互提有一个 $0\sim n-1$ 的排列,保证 ...

  3. react-router 4.0版本学习笔记

    Router 所有路由组件的底层接口,一般情况都不使用,而是使用更加高级的路由. 最常用的有两种<BrowserRouter>.<HashRouter> <Browser ...

  4. 通过Xode上传代码到GIthub---步骤

    ---恢复内容开始--- 一:打开终端,git命令进行全局配置 由于本人已经配置完成,so,直接查看配置信息 然后在本地创建一个文件夹, 然后在gitHub上创建一个代码库 在终端clone到本地创建 ...

  5. Docker 开源项目之 registry - 部署 registry (注册表)服务器

    原文地址 在部署 registry 之前需要现在主机上安装 Docker.registry 实际上就是运行在 Docker 中的 registry 镜像的实例. 本主题提供关于部署和配置 regist ...

  6. 【BZOJ1413】取石子游戏(博弈,区间DP)

    题意:在研究过Nim游戏及各种变种之后,Orez又发现了一种全新的取石子游戏,这个游戏是这样的: 有n堆石子,将这n堆石子摆成一排.游戏由两个人进行,两人轮流操作,每次操作者都可以从最左或最右的一堆中 ...

  7. UIImage与Base64相互转换

    UIImage与Base64相互转换 采用第三方类 Address:https://github.com/l4u/NSData-Base64/ 经测试好用. 2013-07-17

  8. jquery.transform

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. Java下使用Swing来进行图形界面开发

    1. GUI从创建window开始,通常会使用JFrame.JFrame frame = new JFrame(); 2. 你可以这样加入按钮,文字字段等组件.frame.getContentPane ...

  10. 恶补一下DP+背包专题(刷刷水题)L2

    开心的金明 题目大意 就是求一定背包容量的最大值 思路 想必大家都知道,一看到这种题目,就会想起01背包 虽然特别简单但是还是讲一下吧 状态设置 由于这题差不多是一个01背包的版子题,那么我们就只需要 ...