POJ - 3461 Oulipo(KMP模板题)
来源:互联网 发布:阿里云smtp服务器地址 编辑:程序博客网 时间:2024/05/20 02:25
B - Oulipo
Time Limit:1000MS MemoryLimit:65536KB 64bit IO Format:%I64d& %I64u
Submit Status Practice POJ 3461
Description
The Frenchauthor Georges Perec (1936–1982) once wrote a book, La disparition, without theletter 'e'. He was amember of the Oulipo group. A quote from the book:
Tout avait Pairnormal, mais tout s’affirmait faux. Tout avait Fair normal, d’abord, puissurgissait l’inhumain, l’affolant. Il aurait voulu savoir où s’articulaitl’association qui l’unissait au roman : stir son tapis, assaillant à toutinstant 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 commandanttout, où s’abolissait la raison : tout avait l’air normal mais…
Perec wouldprobably have scored high (or rather, low) in the following contest. People areasked to write a perhaps even meaningful text on some subject with as fewoccurrences of a given “word” as possible. Our task is to provide the jury witha program that counts these occurrences, in order to obtain a ranking of thecompetitors. These competitors often write very long texts with nonsensemeaning; a sequence of 500,000 consecutive 'T's is not unusual. And they never use spaces.
So we want toquickly find out how often a word, i.e., a given string, occurs in a text. Moreformally: given the alphabet {'A', 'B', 'C', …, 'Z'} and two finitestrings over that alphabet, a word W and a text T,count the number of occurrences of W in T. All theconsecutive characters of W must exactly match consecutive characters of T.Occurrences may overlap.
Input
The first lineof 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 testcase in the input file, the output should contain a single number, on a singleline: the number of occurrences of the word W in thetext T.
Sample Input
3
BAPC
BAPC
AZA
AZAZAZA
VERDI
AVERDXIVYERDIAN
Sample Output
1
3
0
KMP模板题。
#include<iostream>#include<string>#include<cstring>#include<cstdio>#include <stdlib.h> //题目大意: 给出主串和模式串,求模式串在主串中出现的次数(部分可以重合)using namespace std; //暴力匹配自然会 TLE //用KMP,利用上next[ ]数组的含义,每次不全部退出,而是退出部分j=next[ j ]int nextl[1000000];char str[1000000];char word[100000];int len;int Slen;void get_nextlen(int n){int i = 0, j = -1;nextl[0] = -1;while (i <= n){if (j == -1 || word[i] == word[j]){j++; i++;nextl[i] = j;}else j = nextl[j];}return;}int Kmp(int u){int i = u - 1, j = -1, m = 0; //字符串从0开始存储的while (i <= Slen){if (j == -1 || str[i] == word[j]){i++; j++;}elsej = nextl[j];if (j >= len) //**注意这里要j--,i--,因为上面匹配成功了i和j的值加多了1{m++;j--; i--;j = nextl [j]; //返回}}return m;}int main(){int casen;cin >> casen;while (casen--){memset(nextl, 0, sizeof(nextl));cin >> word;cin >> str;len = strlen(word);Slen = strlen(str);get_nextlen(len);int ans = Kmp(0); //Kmp从第一个字符开始匹配cout << ans << endl;}}
- poj 3461 Oulipo(kmp 模板题)
- poj 3461 Oulipo(KMP模板题)
- POJ - 3461 Oulipo(KMP模板题)
- POJ 3461 Oulipo(KMP模板题)
- POJ:3461-Oulipo(KMP模板题)
- KMP模板题 poj 3461 Oulipo
- 【哈希&KMP模板题】-POJ-3461-Oulipo
- POJ 3461 Oulipo (KMP模板题)
- poj 3461 Oulipo (KMP 模板题)
- POJ 3461 Oulipo [KMP模板题]
- POJ 3461 Oulipo KMP模板题
- POJ 3461 Oulipo(KMP模板)
- POJ 3461 Oulipo (kmp模板)
- POJ 3461 Oulipo(KMP模板)
- POJ 3461 Oulipo(KMP模板)
- POJ Oulipo(KMP模板题)
- poj 3461 Oulipo(KMP,喜闻乐见纯模板)
- POJ 3461 - Oulipo(KMP)
- 值得学习的C语言开源项目
- 观察者模式
- 链式线性表的c++实现
- vc++6.0调试mex文件
- 使用TRY/CATCH 格式
- POJ - 3461 Oulipo(KMP模板题)
- Java NIO简单例子
- Android应用内存泄露分析
- 黑马程序员--OC-关键字
- Android UI 模仿界面框架系列第二章:QQ 聊天界面开发和及时通讯功能开发
- cookie 与session 区别和联系
- Mysql Fabric实现学习笔记
- HDU2680 Choose the best route 最短路
- appframework 左侧和右侧点击滑动显示