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. 独木桥(bridge)

    独木桥(bridge) 题目描述 Alice和Bob是好朋友,这天他们带了n个孩子一起走独木桥. 独木桥宽度很窄,不允许两个或两个以上的人并肩行走,所有人必须要前后一个接一个地通行. Bob给所有的孩 ...

  2. pat 甲级 1010. Radix (25)

    1010. Radix (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a pair of ...

  3. python实现贪吃蛇

    贪吃蛇的算法还是比较简单的,蛇的移动我是通过不停添加一个head方块,然后判断应该加到蛇头的哪个方向,加完后删掉蛇尾就行了,如果吃到食物就不删蛇尾. 只是一个贪吃蛇只需要70行代码左右就可以了,后来又 ...

  4. CODEVS【3556】科技庄园

    题目描述 Description Life是codevs的用户,他是一个道德极高的用户,他积极贯彻党的十八大精神,积极走可持续发展道路,在他的不屑努力下STN终于决定让他在一片闲杂地里种桃,以亲身实践 ...

  5. flash编程实例源代码下载

    原文发布时间为:2008-08-20 -- 来源于本人的百度文章 [由搬家工具导入] http://library.sx.zj.cn/shgp/ActionScript编程实例详解.rar

  6. 【Windows Message】MFC 通过F5,刷新桌面

    //通过F5,刷新桌面 HWND hWndProgram = ::FindWindow( _T("Progman"), NULL); HWND hWndDefView = ::Fi ...

  7. C语言编程中函数指针的定义及使用

    C语言中函数指针的定义: typedef int (*funcPtr)(int, int)表示定义了一个函数指针funcPtr,这个函数指针只能指向如下: int add(int, int).int ...

  8. dhcp 学习整理

    centos 6.5 rpm: dhcp-4.1.1-25.P1.el6.x86_64 dhcp-common-4.1.1-25.P1.el6.x86_64 服务: /etc/rc.d/init.d/ ...

  9. poj 3321(树状数组)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 24954   Accepted: 7447 Descr ...

  10. CString 转换为 char*

    CString str="这是一个测试"; char* str_0=str.GetBuffer(0);