Oulipo

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 21428    Accepted Submission(s): 8324

Problem 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
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  1358 3336 3746 2203 3068 
 
分析:
给你两个字符串,问你这两个字符串的可重叠的匹配次数是多少?
比如AZAZAZA匹配AZA是3次
需要注意的地方:
每次匹配成功之后j不要复位为0,而是需要复位到开始重叠的部分
即j=next[j]
code:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<memory>
using namespace std;
char moban[],wenben[];
int next1[];
int sum;
void getnext(char* s,int* next1,int m)
{
next1[]=;
next1[]=;
for(int i=;i<m;i++)
{
int j=next1[i];
while(j&&s[i]!=s[j])
j=next1[j];
if(s[i]==s[j])
next1[i+]=j+;
else
next1[i+]=;
}
}
void kmp(char* ss,char* s,int* next1,int n)
{
int m=strlen(s);
getnext(s,next1,m);
int j=;
for(int i=;i<n;i++)
{
while(j&&s[j]!=ss[i])
j=next1[j];
if(s[j]==ss[i])
j++;
if(j==m)
{
sum++;
//注意这里j不用复位为0,而是需要等于next[j],可重叠匹配匹配完之后需要跳到开始重叠的部分
j=next1[j];//!!!
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
sum=;
scanf("%s",moban);
scanf("%s",wenben);
int L=strlen(wenben);
kmp(wenben,moban,next1,L);
printf("%d\n",sum);
}
return ;
}

HDU 1686 Oulipo (可重叠匹配 KMP)的更多相关文章

  1. HDU 1686 Oulipo(优化的KMP)

    Oulipo Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  2. HDU - 1686 Oulipo KMP匹配运用

    id=25191" target="_blank" style="color:blue; text-decoration:none">HDU - ...

  3. HDU 1686 Oulipo / POJ 3461 Oulipo / SCU 2652 Oulipo (字符串匹配,KMP)

    HDU 1686 Oulipo / POJ 3461 Oulipo / SCU 2652 Oulipo (字符串匹配,KMP) Description The French author George ...

  4. hdu 1686 Oulipo KMP匹配次数统计

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 分析:典型的KMP算法,统计字符串匹配的次数. 用Next数组压缩时间复杂度,要做一些修改. / ...

  5. HDU 1686 Oulipo (KMP 可重叠)

    题目链接 Problem Description The French author Georges Perec (1936–1982) once wrote a book, La dispariti ...

  6. HDU 1686 Oulipo(KMP+计算匹配成功次数)

    一开始总是超时,后来发现还是方法没找对,这个跟普通KMP不太一样的就是,KMP匹配成功的时候会完全跳过已经匹配成功的匹配段,至少我掌握的是.那么如何避免这样的问题呢,举个栗子啊 原串为ABABA,模式 ...

  7. hdu 1686 Oulipo 【KMP】(计算模式串匹配的次数——与已匹配的字串可以有交集)

    题目链接:https://vjudge.net/contest/220679#problem/B 题目大意: 输入一个T,表示有T组测试数据: 每组测试数据包括一个字符串W,T,T长度大于W小于100 ...

  8. HDU 1686 - Oulipo - [KMP模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 Time Limit: 3000/1000 MS (Java/Others) Memory Li ...

  9. hdu 1686 Oulipo kmp算法

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1686 题目: Problem Description The French author George ...

随机推荐

  1. [CTSC2008]祭祀(构造方案)

    前面的话 这道题显然就是最长反链 根据 \(Dilworth\) 定理:最小链覆盖数 = 最长反链长度 然后传递闭包跑匹配即可 \(luogu\)交了一下,\(WA\) 了 \(QAQ\) 本来各种 ...

  2. jetty插件实现 热部署

    <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin ...

  3. Tinker + Bugly + Jenkins 爬坑之路

    前阵子 Android 端的线上崩溃比较多,热修复被提上日程.实现方案是 Tinker,Jenkins 打包,最后补丁包上传到 Bugly 进行分发.主要在 Jenkins 打包这一块爬了不少坑,现记 ...

  4. jquery 之 $().each和$.each()

    一.选择器+遍历(dom操作)分为两种: 第一种: $('div').each(function (i){ i就是索引值 this 表示获取遍历每一个dom对象 }); <!DOCTYPE ht ...

  5. 算法day02

    算法动态演示网站 数组是有长度限制的,有类型限制 CPU计算 每次是2的32位的,32根线,支持最大是4G 数组:线性表 列表:动态表链表:存储的位置不是连续的 insert 时间复杂度是 Onapp ...

  6. awk单行脚本快速参考

    AWK单行脚本快速参考 2008年4月28日编辑: Eric Pement eric [at] pement.org 版本 0.26翻译: 董一粟 yisudong [at] gmail.com 最新 ...

  7. VS :不会命中断点 代码版本与原始版本不同

    设置了断点,但是无法中断,提示"不会命中断点 代码版本与原始版本不同".这种情况下一般是生成的bin\debug下面的文件与实际代码不符. 但是这次确实没有问题,重新更新程序,清理 ...

  8. [翻译] SvpplyTable

    SvpplyTable https://github.com/liuminqian/SvpplyTable SvpplyTable is a demo to realize expandable an ...

  9. How to rename table name

    How to rename table name eg.  rename emp to emp01; see aslo: https://docs.oracle.com/javadb/10.8.3.0 ...

  10. 沉淀再出发:关于IntelliJ IDEA使用的一些总结

    沉淀再出发:关于IntelliJ IDEA使用的一些总结 一.前言 在使用IDEA的时候我们会发现,如果我们先写了一个类的名字,而没有导入这个类的出处,就会提示出错,但是不能自动加入,非常的苦恼,并且 ...