KMP算法: Oulipo
这是一道北大OJ上的道
题目描述
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| ≤ 20,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| ≤ 2,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.
样例输入
3
BAPC
BAPC
AZA
AZAZAZA
VERDI
AVERDXIVYERDIAN
样例输出
1
3
0
分析:
使用KMP算法匹配字符串
#include <bits/stdc++.h>using namespace std;string sam,t;
int nxt[1000050];
int sl,tl;void getnxt(){//求next数组int j = 0,k = -1;nxt[0] = -1;while(j < tl){if(k == -1||t[j] == t[k])nxt[++j] = ++k;else k = nxt[k];}
}int kmpcount(){//找总匹配数int res = 0;int i = 0;int j = 0;if(sl == 1&&tl == 1){if(sam[0] == t[0])return 1;else return 0;}getnxt();for(i = 0;i < sl;i++){while(j>0&&sam[i]!=t[j]){j = nxt[j];}if(sam[i] == t[j])j++;if(j == tl){res++;j = nxt[j];}}return res;
}int main(){int W;cin>>W;for(int l = 0;l<W;l++){cin>>t>>sam;sl = sam.size();tl = t.size();cout<<kmpcount()<<endl;}return 0;
}
KMP算法: Oulipo相关推荐
- A - Oulipo(KMP算法经典)
详细KMP见http://blog.csdn.net/u014665013/article/details/37995355 Description The French author Georges ...
- Oulipo (KMP算法)
Oulipo (KMP算法) 题目链接:HDU-1686 题目: Oulipo Problem Description The French author Georges Perec (1936–19 ...
- Oulipo(kmp算法)
题目意思就是找子串在一个长串中出现的次数. 因为数据较大,一般的做法都超时,看了看kmp算法,用这个算法做的.这个算法最难的就是求那个next数组吧. #include<iostream> ...
- POJ3461 Oulipo ——KMP算法——Pku3461
建议大家学一学比较巧妙的KMP算法吧,很有意思.推荐个题目:POJ3167 Cow Patterns 题解我会发在本博里. 这个KMP就木有什么好说的了吧,大家找百度百科学一下就可以了~ CODE P ...
- POJ 3461 Oulipo(kmp算法解析)
题目链接:https://cn.vjudge.net/contest/320014#problem/F Sample Input 3 BAPC BAPC AZA AZAZAZA VERDI AVERD ...
- BF算法优化-------KMP算法
百度百科:KMP算法是一种改进的字符串匹配算法,由D.E.Knuth,J.H.Morris和V.R.Pratt提出的,因此人们称它为克努特-莫里斯-普拉特操作(简称KMP算法).KMP算法的核心是利用 ...
- 数据结构与算法(5)字符串(BF算法、KMP算法及KMP算法优化)
目录 一.BF算法(暴力算法) 二.KMP算法 三.KMP算法优化 一.BF算法(暴力算法) 一个一个往后匹配,匹配失败继续从母串下一个和头(子串的头)往后继续匹配. 虽然简单,但是需要较多的时间复杂 ...
- hiho 1015 KMP算法 CF 625 B. War of the Corporations
#1015 : KMP算法 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在 ...
- HDU 1711 Number Sequence(KMP算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/ ...
最新文章
- title和alt属性
- 年过35岁的程序员都去哪儿了
- 【js】typeof与instanceof
- 角点检测:Harris角点及Shi-Tomasi角点检测
- C++下关于XML开源库的使用
- python中的单引号双引号和三引号
- OpenStack vlan教程 (操作篇)
- HTML5网络科技企业网页模板
- 面向对象程序设计简介
- 安防视频之web无插件播放视频
- Python的字符串格式化 %r %s
- 我的世界服务器怎么修改标语,怎么用资源包修改我的世界闪烁标语
- unity3D人物动画
- Netcore磊科NW336驱动Win7下载 NW336 150M无线USB网卡驱动
- PS多色复杂图片上去除文字(保姆级别教程)
- 华硕服务器 u盘安装系统,华硕电脑u盘安装系统教程
- 刑事案件的构成要素 zt
- linux文件权限管理实验心得,Linux+文件权限管理实验
- hadoop3.x\2.x常用端口,和常用配置文件
- Unity 图片定点缩放功能
热门文章
- 学计算机的能制造芯片,黑科技:木头造计算机芯片 还能降解
- python微博爬虫实战_爬虫实战(一):爬取微博用户信息
- 信息系统项目管理师学习笔记7-项目成本管理
- Linux Cobbler批量装机
- 毕业论文的流程图怎么画
- word分散对齐调整宽度_实例比较Word两端对齐与分散对齐
- 【文献阅读】Silhouette based View embeddings for Gait Recognit
- Android实现仿支付宝流水
- linux 网桥 权限,如何在 Ubuntu 上搭建网桥
- E-R 图的不同表示方法(Chen 方法和 Crow‘s foot 方法)