Oulipo (kmp)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 26857 | Accepted: 10709 |
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<cstdio>
#include<cstring>
char t[ + ] , s[ + ] ;
int nxt [ + ] ;
int T ;
int lens , lent ; void PMT ()
{
nxt [] = - ;
for (int i = , j = - ; i < lent ; i++) {
while (j != - && t[i] != t[j + ] ) {
j = nxt[j] ;
}
nxt[i] = t[i] == t[j + ] ? ++ j : j ;
}
} void kmp ()
{
int ret = ;
PMT () ;//partial match table
for (int i = , j = - ; i < lens ; i++) {
while (j != - && s[i] != t[j + ] ) {
j = nxt [j] ;
}
if (s[i] == t[j + ]) {
j ++ ;
if (j == lent - ) {
j = nxt [j] ;
ret ++ ;
}
}
}
printf ("%d\n" , ret );
} int main ()
{
// freopen ("a.txt" , "r" , stdin ) ;
scanf ("%d" , &T) ;
getchar () ;
while (T--) {
memset (nxt , , sizeof(nxt) ) ;
gets (t) ;
gets (s) ;
lens = strlen (s) ;
lent = strlen (t) ;
// puts (t) , puts (s) ;
kmp () ;
}
return ;
}
Oulipo (kmp)的更多相关文章
- poj3461 Oulipo(KMP模板)
Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17795 Accepted: 7160 Descripti ...
- hdu 1686 Oulipo KMP匹配次数统计
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 分析:典型的KMP算法,统计字符串匹配的次数. 用Next数组压缩时间复杂度,要做一些修改. / ...
- [POJ] 3461 Oulipo [KMP算法]
Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 23667 Accepted: 9492 Descripti ...
- hdu Oulipo(kmp)
Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, w ...
- hdu 1686 Oulipo (kmp)
Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, w ...
- hdu1686 Oulipo KMP/AC自动机
The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e ...
- HDU 1686 - Oulipo - [KMP模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 Time Limit: 3000/1000 MS (Java/Others) Memory Li ...
- HDU 1686 Oulipo (KMP 可重叠)
题目链接 Problem Description The French author Georges Perec (1936–1982) once wrote a book, La dispariti ...
- hdu 1686 Oulipo kmp算法
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1686 题目: Problem Description The French author George ...
随机推荐
- 海量数据处理利器之Hash——在线邮件地址过滤
标题用了了海量数据(Massive datasets)而不用大数据(Big data).感觉大数据还是略微有点虚,来点实际的. 一.需求 现在我们需要设计一个在线过滤垃圾邮件地址的方案,我们的数据库里 ...
- Android中的Intent Filter匹配规则介绍
本文主要介绍了隐式Intent匹配目标组件的规则,若有叙述不清晰或是不准确的地方希望大家指出,谢谢大家: ) 1. Intent简介 Intent用于在一个组件(Component,如Activity ...
- 【开源】XPShadow, 用阴影让UWP更有层次感
UWP采用的是纯扁平化的设计,个人感觉极端了点,整个世界都是平的,导致App分不清层次,看不出重点.其实扁平化是趋势,android, ios都在搞,问题是android, ios都可以很轻松的实现阴 ...
- HTML5 中canvas支持触摸屏的签名面板
1.前言 最近实在是太忙了,从国庆之后的辞职,在慢慢的找工作,到今天在现在的这家公司上班大半个月了,太多的心酸泪无以言表,面试过程中,见到的坑货公司是一家又一家,好几家公司自己都只是上一天班就走了,其 ...
- 怎样写 OpenStack Neutron 的 Plugin (一)
鉴于不知道Neutron的人也不会看这篇文章,而知道的人也不用我再啰嗦Neutron是什么东西,我决定跳过Neutron简介,直接爆料. 首先要介绍一下我的开发环境.我没有使用DevStack,而是直 ...
- 谏牲口TT十思疏
予闻:求木之长着,必固其根本:欲流之远者,必浚其泉源:思吾之长者,必积其学识.源不深而望流之远,根不固而求木之长,识不积而思指日之安,斯虽下愚,知其不可,而况于TT乎?TT当举家之重,虑只此一生,将孝 ...
- 公司ERP系统重构那些事
记一次会议,我提出插件化的想法,有支持也有反对,其中"系统架构师"表示插件化后的项目没什么意义,今天来讨论项目是否需要插件化的一些观点. 项目背景 公司内部"ERP&qu ...
- 10、面向对象以及winform的简单运用(isMdicontainer的设置、timer控件进行倒计时的制作)
IsMdicontainer的设置 这是对于整个窗体的设置,将一个窗体的IsMdicontainer设置为true之后,再打开新窗体便可以让新窗体被父容器包括在内. 操作方法: 1)先建立一个子窗体C ...
- Codeforces Round #381 (Div. 2) D. Alyona and a tree 树上二分+前缀和思想
题目链接: http://codeforces.com/contest/740/problem/D D. Alyona and a tree time limit per test2 secondsm ...
- 【Gym 100610A】Alien Communication Masterclass
题 Andrea is a famous science fiction writer, who runs masterclasses for her beloved readers. The mos ...