POJ
来源:互联网 发布:led编辑软件 编辑:程序博客网 时间:2024/06/05 07:29
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.
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.
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.
3BAPCBAPCAZAAZAZAZAVERDIAVERDXIVYERDIAN
130
题意那么长,其实都没有用,就是让你看看模式串在文本串中出现的次数。裸的 KMP,讲讲 KMP把 ,我所理解的KMP就是现在模式串中做 最大前后缀匹配,然后知道在每一个位置i的 最长前后缀长度,之后在和文本串匹配的时候 ,当每次匹配失败后 ,我们就可以移动到和他最长前缀相同的最长后缀的位置接着进行匹配,这样的话我们的文本指针就不会在“回溯” ,大大提高匹配的效率。
上代码把:
#include <stdio.h>#include <string.h>char ch[10000],Ch[1000007];int nex[10020];void pre_kmp(char str[]){int k=-1,i=0;int len = strlen (str);nex[0] = -1;while(i < len){if( k == -1 ||str[i] == str[k]){i++;k++;nex[i] = k;}else {k = nex[k];}}} int kmp(char str[],char Str[]) { int ans=0; int Len = strlen (Str); int len = strlen (str); int k = 0 , i = 0;//记得这里和建立的 nex数组不一样 要从0 开始匹配 while(i <= Len) { if(k == -1 || str[k] == Str[i]) { k++; i++; } else { k = nex[k]; }if(k == len) { ans++; } } return ans; }int main(){int n;scanf("%d",&n);while(n--){memset(nex,0,sizeof(nex));scanf("%s %s",ch,Ch);pre_kmp(ch);printf("%d\n",kmp(ch,Ch));}}
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- 笔试_前端基础综合题
- ArrayList、Vector、HashMap、HashSet的默认初始容量、加载因子、扩容增量
- < 笔记 > HTML
- 大话网络专业术语
- 修改mysql 数据库文件默认存储目录
- POJ
- Oracle序列
- patition布局
- ROS下 orbslam2 双目 (三)
- 知识点篇:1.1)不同目标,不同的设计套路(正向还是逆向)
- 人脸识别-SphereFace
- 为arcgis for js封装自己的map
- Windows mysql 出现access denied for user root @localhost
- linux 中查看info