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 ...
随机推荐
- CS:APP2e Y86处理器模拟器∗指南
CS:APP2e Y86处理器模拟器∗指南 Randal E.Bryant David R. O'Hallaron 2013年7月29日 本文档描述了处理器模拟器,伴随的表示在第4章Y86处理器架构的 ...
- EntityFramework中的线程安全,又是Dictionary
继上次记一次w3wp占用CPU过高的解决过程(Dictionary和线程安全)后又再次与Dictionary博弈,这一次是在EntityFramework中的Dictionary. 从一个异常说起 这 ...
- Address already in use: JVM_Bind<null>:80
Address already in use: JVM_Bind<null>:80 咱还是闲话少说,直接切入正题. 起因: 一直用Tomcat,但是前几天突然报错: java.net.Bi ...
- 整数划分 (区间DP)
整数划分(四) 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 暑假来了,hrdv 又要留学校在参加ACM集训了,集训的生活非常Happy(ps:你懂得),可是他最近 ...
- hdu1595 dijkstra+枚举
开始的时候想的比较简单,直接枚举所有输入的边,但最后超时:后来就先进行一次dij,记录所有最短路上的边,然后枚举删去这些边: #include<stdio.h> #include<s ...
- 反射工具类——ReflectUtils
测试方法:根据已知的方法名,来反射调用方法 package com.redmoon.forum.job; import java.util.ArrayList; import java.util.Li ...
- Java设计模式-单例模式(Singleton)
单例对象(Singleton)是一种常用的设计模式.在Java应用中,单例对象能保证在一个JVM中,该对象只有一个实例存在.这样的模式有几个好处: 1.某些类创建比较频繁,对于一些大型的对象,这是一笔 ...
- yii2图片上传
yii2利用自带UploadedFile上传图片 public static function uploadFile($name) { $uploadedFile = UploadedFile::ge ...
- Oracle分页查询语句
SELECT * FROM (SELECT A.*, ROWNUM RN FROM (SELECT * FROM (此处添加你要分页的表)) A WHERE ROWNUM <= 14000)WH ...
- 【BZOJ-2879】美食节 最小费用最大流 + 动态建图
2879: [Noi2012]美食节 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1366 Solved: 737[Submit][Status] ...