poj 3461 (看毛片算法初探)
来源:互联网 发布:蜂窝数据用量0 编辑:程序博客网 时间:2024/04/27 06:58
Oulipo
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
Submit
Status
Practice
POJ 3461
Appoint description:
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.
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.
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
3
BAPC
BAPC
AZA
AZAZAZA
VERDI
AVERDXIVYERDIAN
Sample Output
1
3
0
KMP模板题了
#include <cstdio>#include <cstring>#define M 10010#define N 1000010char str[N], ptr[M];//str 主串,ptr 模板串int next[M], plen, slen, num;//next[i]就是存储当模式串在第 i 处失配时,下次该匹配的位置//next[i] = -1 的意思是模式串与主串应该重新在 i+1 地方重新匹配void getnext(){ int i = 0, k = -1; next[0] = -1; while(i < plen) { if(k == -1 || ptr[i] == ptr[k])//匹配成功 { i++, k++; if(ptr[i] == ptr[k]) { next[i] = next[k]; } else { next[i] = k;//由前缀与后缀匹配的情况,得到此处失配时应该再次对比的位置 } } else { k = next[k];//失配回溯,这个地方很难理解,可以认为回到了一个前缀与后缀可以相等的地方,或者直接回到了 -1 重新匹配的点 //在这个地方想不通呢,对比一下getnext函数与kmp函数,可以发现这个相当于又回到了求主串与模板串的地方,模式串就等于主串,匹配不成功就next //就理解为把当前的模式串看成主串,回溯到得那个串看成子串,然后再次匹配,一直循环,知道 k == -1;跳过,进入下一段 } }}void kmp(){ int pi = 0, si = 0;//从开始的地方比较 while(si < slen) { if(pi == -1 || str[si] == ptr[pi])//匹配成功 { pi++, si++; } else//匹配失败 { pi = next[pi];//回溯到应该重新匹配的位置 } if(pi == plen)//完全匹配成功 { pi = next[pi];//回溯 num++;//计数 } } printf("%d\n", num);}int main(){ int t; scanf("%d", &t); while(t--) { scanf("%s", ptr); scanf("%s", str); slen = strlen(str); plen = strlen(ptr); num = 0; getnext(); kmp(); } return 0;}
- poj 3461 (看毛片算法初探)
- 看毛片(KMP)算法简析
- 字符串匹配KMP(看毛片)算法
- KMP-看毛片算法
- kmp(看毛片)算法详解
- KMP-看毛片算法 c++
- 初次接触kmp看毛片算法
- kmp“看毛片”算法模板题
- 字符串匹配之看毛片算法-KMP
- KMP (看毛片算法) 养成篇 (一)(启发篇)
- 菜鸟都能理解的看毛片(KMP)算法
- KMP(看毛片)-
- 【字符串匹配】——KMP(看毛片算法)——深入讲解next数组的求解
- HDU--杭电--1711--Number Sequence--KMP--水题,看毛片算法基础死方法运用
- 萌新的看毛片(kmp)学习日记(下标从0开始)
- 那些年,我们一起看的毛片
- POJ初探
- 初探圆周率算法(转载)
- 数据库中常用的sql语句
- JDBC学习(一)
- JAVA文件操作类和文件夹的操作
- 如何动态加载apk文件(未安装的apk)中的类
- iOS 远程消息推送 APNS推送原理和一步一步开发详解篇
- poj 3461 (看毛片算法初探)
- PHP编程效率的20个要点
- AForge获取本机视频设备列表
- liunx 录制命令 script
- 卸载LoadRunner及破解
- redis与mysql数据同步
- hdu1372-Knight Moves BFS
- STM32 day4
- SDUTACM 图结构练习——BFS——从起始点到目标点的最短步数