[HDOJ1686]Oulipo
2015-08-24 13:49
316 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686
Total Submission(s): 7716 Accepted Submission(s): 3102
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
3
BAPC
BAPC
AZA
AZAZAZA
VERDI
AVERDXIVYERDIAN
[align=left]Sample Output[/align]
1
3
0
还是KMP,和上一个题一样。就当时熟练一下KMP。要小心的是这回匹配到最后一个字符,并且注意输入T以后要吸收掉\n。还有目标串和模式串的位置,跟上一个题也不一样。
Oulipo
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7716 Accepted Submission(s): 3102
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
3
BAPC
BAPC
AZA
AZAZAZA
VERDI
AVERDXIVYERDIAN
[align=left]Sample Output[/align]
1
3
0
还是KMP,和上一个题一样。就当时熟练一下KMP。要小心的是这回匹配到最后一个字符,并且注意输入T以后要吸收掉\n。还有目标串和模式串的位置,跟上一个题也不一样。
#include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <iostream> #include <cmath> #include <queue> #include <map> #include <stack> #include <list> #include <vector> using namespace std; const int maxn = 1000010; int na, nb; char a[maxn]; char b[maxn]; int pre[maxn]; void getpre(char *b, int *pre) { int j, k; pre[0] = -1; j = 0; k = -1; while(j < nb) { if(k == -1 || b[j] == b[k]) {//匹配 j++; k++; pre[j] = k; } else { //b[j] != b[k] k = pre[k]; } } } int kmp() { int ans = 0; int i = 0; int j = 0; getpre(b, pre); while(i < na) { if(j == -1 || a[i] == b[j]) { i++; j++; } else { j = pre[j]; } if(j == nb) { ans++; } } return ans; } int main() { int T; scanf("%d", &T); getchar(); while(T--) { memset(pre, 0, sizeof(pre)); gets(b); gets(a); na = strlen(a); nb = strlen(b); printf("%d\n", kmp()); } return 0; }
相关文章推荐
- bower和npm的依赖管理
- Android5.0 ripple效果低版本的实现
- bzoj2708木偶(dp)
- Android 内存优化解决方案 (OOM)
- Android学习系列(1)--为App签名(为apk签名)
- [LeedCode OJ]#91 Decode Ways
- nignx安装
- 用angular来思考问题How do I “think in AngularJS” if I have a jQuery background?
- 【LeetCode】9. Palindrome Number
- 你写的前端到底用没用到这些
- C#软件winform程序安装包制作及卸载程序制作
- jsonc库使用
- busybox 安装
- tomcat无法连接,./shutdown.sh的时候报connect refuse
- Hibernate乐观锁和悲观锁
- TLD算法流程说明--episode2
- 利用模板导出文件(一)之XLSTransformer导出excel文件
- 小议:部署SharePoint 2013时,无法连接SQL Server问题
- linux centos6.5 修改ip地址
- c语言解析json数据