hdu 1686 Oulipo -- (KMP模板题)
来源:互联网 发布:江汉大学网络支付平台 编辑:程序博客网 时间:2024/06/05 15:55
Oulipo
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 9814 Accepted Submission(s): 3910
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.
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.
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
3BAPCBAPCAZAAZAZAZAVERDIAVERDXIVYERDIAN
Sample Output
130
#include <stdio.h>#include <string.h>#define N 1000005char t[N],w[N]; //t是主串int next[N];void get_next(char *a,int len){next[0]=-1;int i=0,j=-1;while(i<len){if(j==-1||a[i]==a[j]){if(a[++i]==a[++j]) next[i]=next[j];else next[i]=j;}else j=next[j];}}int KMP(char *a,char *b){ //a是主串int i=0,j=0;int sum=0;int lena=strlen(a);int lenb=strlen(b);get_next(b,lenb);while(i<lena&&j<lenb){if(j==-1||a[i]==b[j]) i++,j++;else j=next[j];if(j==lenb){sum++;j=next[j]; //可以重叠//j=0 不可以重叠}}return sum;}int main(){int n;scanf("%d",&n);while(n--){scanf("%s%s",&w,&t);printf("%d\n",KMP(t,w));}return 0;}
0 0
- hdu 1686 Oulipo -- (KMP模板题)
- HDU 1686Oulipo (kmp模板题)
- HDU-1686 Oulipo(kmp模板题)
- HDU 1686 Oulipo【KMP模板题】
- hdu 1686 Oulipo (KMP模板)
- HDU 1686 Oulipo kmp模板
- HDU 1686 Oulipo(kmp)
- HDU 1686 Oulipo(KMP)
- hdu 1686 Oulipo (kmp)
- HDU - 1686 Oulipo(KMP)
- HDU 1686 Oulipo (kmp)
- hdu 1686 Oulipo(KMP)
- HDU 1686 Oulipo(kmp)
- HDU 1686 Oulipo(KMP)
- hdu-1686-Oulipo(KMP)
- HDU 1686 Oulipo (kmp)
- hdu 1686 kmp Oulipo
- KMP hdu-1686-Oulipo
- Python中的字符编码问题
- nyoj 1058 部分和问题
- C++ Socket编程之小试牛刀
- 第六次作业
- 类小结3: 接口 内部类
- hdu 1686 Oulipo -- (KMP模板题)
- LSD-FET430UIF仿真器问题
- C++版本的C标准库头文件的特点
- ListView常用属性、方法
- Eclipse新建C project不会自动生成Includes文件夹的解决办法
- Spark 【数据挖掘平台介绍】 - Spark 1.1.0(看范式粒度)
- equals方法和==方法比较
- 关于Linux中的Profile文件
- uva 122 Trees on the level