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 ...
随机推荐
- 问卷调查——答卷
1.你对自己的未来有什么规划?做了哪些准备?答:对未来的规划,就是像大多数人一样,在学校里有一个好的成绩,掌握扎实的专业基础,然后在国考上得到一个好成绩,得到一个好的工作机会,然后努力工作.目标很简单 ...
- ViewController与outlet绑定
ViewController的作用 ViewController与XIB一一对应,用于分离独立出可重用组件单元,如单个组件.复合组件.界面片段.整个界面等. 通常继承 UIViewController ...
- sql server 2008 操作数据表
SQL Server表 表的类型: ①临时表 临时表可用来处理中间数据或者用临时表 与其它连接共享进行中的工作.临时表只 能放在tempdb中. 私有临时表(#) 全局临时表(##) ②系 ...
- JavaScript 里 new 出来的对象 怎么销毁它?
JavaScript的规范(ECMA-262 ECMAScript)没规定JavaScript引擎要如何实现对JavaScript对象的内存管理.实际实现中几乎所有JavaScript引擎都使用基于跟 ...
- jQuery找兄弟系列next(),nextAll(),nextUntil(),prev(),prevAll(),prevUntil(),siblings()
<body> <div id="main"> <div id="hot" class="rightbar"&g ...
- 05.C#泛型类型(三章3.1-3.2)
接下来说下C#中的泛型,熟练地使用泛型能提高代码的重用性,使用我们代码瞬间就高大上了,当然只有一点点,真的只有一点点,因为后面要学习和掌握的知识还有很多.先来看下一个使用Dictionary<T ...
- mysqld参数配置
这个文件超级大, 查了一下, 大概的作用如下 是储存的格式INNODB类型数据状态下,ibdata用来储存文件的数据而库名的文件夹里面的那些表文件只是结构而已 由于mysql4.1默认试innodb, ...
- android学习——Android Studio下创建menu布局文件
一.问题: android studio项目中没有看到menu文件夹: 在android studio项目中想要添加menu布局文件,一开始我的做法是:直接在res文件夹右键选择xml文件来添加,如下 ...
- 设置java jvm(虚拟机) 的内存在大小
package WanWan; public class Test { /** * @param args */ public static void main(String[] args) { // ...
- HTML5的拖拽时间 ondragstart
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...