POJ - 3461 (kmp)
题目链接:http://poj.org/problem?id=3461
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 49608 | Accepted: 19699 |
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
Source
#include<iostream> #include<string.h> #include<map> #include<cstdio> #include<cstring> #include<stdio.h> #include<cmath> #include<ctype.h> #include<math.h> #include<algorithm> #include<set> #include<queue> typedef long long ll; using namespace std; const ll mod=1000; const int maxn=1e6+10; const int maxk=5e3+10; const int maxx=1e4+10; const ll maxe=1000+10; #define INF 0x3f3f3f3f3f3f #define Lson l,mid,rt<<1 #define Rson mid+1,r,rt<<1|1 int next[maxn]; void cal_next(char a[]) {int len=strlen(a);next[0]=-1;int k=-1;for(int i=1;i<len;i++){while(k>-1&&a[k+1]!=a[i]){k=next[k];}if(a[k+1]==a[i]) k++;next[i]=k;} } void kmp(char a[],char b[]) {int k=-1,sum=0;int len1=strlen(a);int len2=strlen(b);for(int i=0;i<len2;i++){while(k>-1&&a[k+1]!=b[i]){k=next[k];}if(a[k+1]==b[i]) k++;if(k==len1-1){sum++;k=next[k];}}printf("%d\n",sum);//cout<<sum<<endl; } int main() {//string a,b;char a[maxn],b[maxn];int t;//cin>>t;scanf("%d",&t);while(t--){//cin>>a>>b;scanf("%s",a);getchar();scanf("%s",b);cal_next(a);kmp(a,b);}return 0; }
转载于:https://www.cnblogs.com/caijiaming/p/9656590.html
POJ - 3461 (kmp)相关推荐
- A - Expanding Rods POJ - 1905(二分)
A - Expanding Rods POJ - 1905(二分) 题目 Problem Description When a thin rod of length L is heated n deg ...
- JavaScript实现knuth-morris-pratt(KMP)算法(附完整源码)
JavaScript实现knuth-morris-pratt(KMP)算法(附完整源码) knuthMorrisPratt.js完整源代码 knuthMorrisPratt.test.js完整源代码 ...
- Oulipo(kmp)
Oulipo(kmp) The French author Georges Perec (1936–1982) once wrote a book, La disparition, without t ...
- 杭电ACM-LCY算法进阶培训班-专题训练(KMP)
杭电ACM-LCY算法进阶培训班-专题训练(KMP) 杭电ACM-LCY算法进阶培训班-专题训练(KMP) 剪花布条 Problem Description Input Output Sample I ...
- Python:实现knuth morris pratt(KMP)算法(附完整源码)
Python:实现knuth morris pratt(KMP)算法 from __future__ import annotationsdef kmp(pattern: str, text: str ...
- poj 3167(KMP+树状数组)
之前自己在做题的时候在网上找别人的题解,虽然当时理解,但时间一久就忘了.所以开个这个东西来记录自己的学习进程,方便自己的回顾,以及给他人提供题解. 开始冲击明年高二的省选!不再颓废! 好了,下面进入正 ...
- LeetCode 1392. 最长快乐前缀(KMP)
1. 题目 「快乐前缀」是在原字符串中既是 非空 前缀也是后缀(不包括原字符串自身)的字符串. 给你一个字符串 s,请你返回它的 最长快乐前缀. 如果不存在满足题意的前缀,则返回一个空字符串. 示例 ...
- 第四章:2.串 -- 串的模式匹配算法(KMP)
前言: 目录: 1.串类型的定义 2.串的表示和实现 3.串的模式匹配算法 4.串操作应用举例 正文: 串的模式匹配即,在给定主串S 中,搜索子串T 的位置,如果存在T 则返回其所在位置,否则返回 0 ...
- 字符串匹配算法(KMP)
文章目录 1. KMP由来 2. KMP算法基本原理 3. 代码 4. Leetcode 28. 实现 strStr() 1. KMP由来 上一节说的BM算法是最高效.最常用的字符串匹配算法. 最知名 ...
最新文章
- 屏幕滚动控件Scrollview
- python yield 简单用法_通过实例简单了解python yield使用方法
- 5, Data Augmentation
- bat代码雨代码流星_bat-入门系列-03-判断结构2
- ORA-01476: divisor is equal to zero解决方法
- excel第一次打开报错 向程序发送命令时出错 多种解决办法含终极解决方法
- ORACLE 日期转换
- (数据库篇) SQL查询~ 存在一个表而不在另一个表中的数据
- 电机计算机仿真,电机调速系统的计算机仿真
- BugKu CTF(杂项篇MISC)---哥哥的秘密
- Bootstrap实战---footer处理
- js中判断数据类型的方法
- Python_封装案例(小明爱跑步)
- 对盖得排行APP内容方面的一些看法
- Cocos Creator基于热更新的分包方案
- 软件配置管理(一)配置管理概念与目标
- C/C++笔试必须熟悉掌握的头文件系列(九)——string
- 再向子公司输血数十亿,这家巨头的汽车电子业务何时迎来拐点?
- SQL Server中替换函数stuff、replace的使用
- 物联网中你需要了解的ESP8266最基本的知识!
热门文章
- MVC中如何实现本地化的解决方案
- linux----------今天又遇到一个奇葩的问题,就是linux文件的权限已经是777了但是还是没有写入权限,按照下面的命令就解决了
- 开源开放的知识图谱工具和数据生态
- 【干货】京东电商推荐系统的应用实践.pdf(附下载链接)
- 【报告分享】数据资产化之路----数据资产的估值与行业实践.pdf
- 浙大 PAT b1022
- python tablewidget 颜色_更改QTableWidget的默认选择颜色,并使其半透明
- python网络爬虫系列教程_Python网络爬虫系列教程连载 ----长期更新中,敬请关注!...
- PHP心脏装置,“人工心脏”不再科幻 “钢铁侠”已成现实
- Leetcode每日一题:129.sum-root-to-leaf-numbers(求根到叶子节点数字之和)