1686 Oulipo【kmp】
2015-08-07 15:58
405 查看
Oulipo
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7282 Accepted Submission(s): 2913
[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 算法自己不太理解,现在大概知道这个算法的思路和运行的步骤了,但是具体为什么这么做,还没想出来,整个算法最精华的就是 数组 p 的元素的代表意义,如果这个能理解,整个算法也就理解了,网上的一个答案是.....
数组 p (或者说 next 数组)中保存的是该失配字符的前一个字符在前面出现过的最近一次失配的字符后面的字符的位置-----这是网上的原话,自己还是不太明白,只知道整个p数组里保存的是某个字符的位置,具体什么情况还需要自己以后慢慢理解......
大概说一下自己的理解,kmp 算法需要先字串和字串进行自身匹配,求出自身的特征数组p(储存着该字符串的某种特征),然后,利用数组 p ,两个工作指针一起对主字符串和子字符串进行匹配......具体为什么这么操作,也不是很清楚,自己也在思考....
#include<stdio.h> #include<string.h> #define m 10005 char x[m],y[m*100]; int len1,len2,p[m],cnt; void getp()//自身和自身比较,找某种特征 { int i=0,j=-1; p[0]=-1;//第一位赋值 while(i<len1) { if(j==-1||x[i]==x[j])//匹配成功的话,继续下一位 { ++i;++j; p[i]=j; } else//否则返回到可以匹配的位置 { j=p[j]; } } } void kmp() { getp(); int i=0,j=0; while(i<len2) { if(j==-1||y[i]==x[j])//进行两个字符串的匹配 { ++i,++j;//匹配成功,往后继续匹配 if(j==len1)//匹配完成一次,代表出现了一次,记录下来 { ++cnt; } } else//匹配失败,寻找数组中可以匹配的位置 { j=p[j]; } } } int main() { int t; scanf("%d",&t); while(t--) { getchar(); scanf("%s%s",x,y); len1=strlen(x); len2=strlen(y); cnt=0; kmp();//运行匹配函数.... printf("%d\n",cnt); } return 0; }
相关文章推荐
- ORA-12704: 字符集不匹配
- Win10激活失败出现错误代码0xC004C003原因:微软服务器不堪重负
- Linux - sed 工具
- 关于中国教育的反思
- 字符串数组
- hdu4513 吉哥系列故事——完美队形II
- Linux - sed 工具
- BFS
- HTML5绘制颜色渐变
- JSON字符串和表互相转换
- centos 配置本地yum源
- Team Queue(POJ 2259)
- 第18章 多线程----线程简介
- 【暑假】[实用数据结构]UVAlive 4329 Ping pong
- c++内存池
- Shell中括号的作用
- springmvc+mybatis的配置信息
- Drupal 模块的 Hooks(钩子)
- Java子类继承父类
- 模板方法(Template)设计模式