题解报告:hdu 1686 Oulipo(裸KMP)
Problem Description
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
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
Sample Input
Sample Output
#include<cstdio>
#include<string.h>
const int maxn=1e4+;
const int maxm=1e6+;
char text[maxm],pattern[maxn];
int prefix[maxn],lena,lenb,num,t;
void get_prefix_table(){//处理模式串前缀表
int j=,pos=-;
prefix[]=-;
while(j<lenb){
if(pos==-||pattern[pos]==pattern[j])prefix[++j]=++pos;
else pos=prefix[pos];
}
}
void kmp_search(){
int i=,j=;
while(i<lena){
if(j==-||text[i]==pattern[j])i++,j++;
else j=prefix[j];
if(j==lenb)num++,j=prefix[j];//此时的j应退回到j前面子串的最长公共前后缀长度的位置进行下一次匹配,可以有相交的模式串
}
}
int main(){
while(~scanf("%d",&t)){
while(t--){
scanf("%s%s",pattern,text);
memset(prefix,,sizeof(prefix));
num=,lena=strlen(text),lenb=strlen(pattern);
get_prefix_table();
kmp_search();
printf("%d\n",num);
}
}
return ;
}
题解报告:hdu 1686 Oulipo(裸KMP)的更多相关文章
- HDU 1686 Oulipo(KMP)题解
题意:主串中能找到几个模式串 思路:超详细解释KMP KMP:针对这个代码,解释一下Fail数组的含义:T为主串,P为模式串,Fail代表失配值,即当P[j] != T[i]时,j要指向的位置为Fai ...
- HDU 1686 Oulipo(KMP变形求子串出现数目(可重))
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 题目大意:给两个字符串A,B求出A中出现了几次B(计算重复部分). 解题思路:稍微对kmp()函 ...
- hdu 1686 Oulipo (kmp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 题目大意:寻找子链在母链中出现的次数. #include <iostream> #i ...
- HDU 1686 Oulipo(kmp)
Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, w ...
- hdu 1686 Oulipo 【KMP】(计算模式串匹配的次数——与已匹配的字串可以有交集)
题目链接:https://vjudge.net/contest/220679#problem/B 题目大意: 输入一个T,表示有T组测试数据: 每组测试数据包括一个字符串W,T,T长度大于W小于100 ...
- HDU 1686 Oulipo(KMP+计算匹配成功次数)
一开始总是超时,后来发现还是方法没找对,这个跟普通KMP不太一样的就是,KMP匹配成功的时候会完全跳过已经匹配成功的匹配段,至少我掌握的是.那么如何避免这样的问题呢,举个栗子啊 原串为ABABA,模式 ...
- HDU 1686 Oulipo【kmp求子串出现的次数】
The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e ...
- 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 ...
- HDU - 1686 Oulipo KMP匹配运用
id=25191" target="_blank" style="color:blue; text-decoration:none">HDU - ...
随机推荐
- 【IntelliJ】IDEA使用--字体、编码和基本设置
IDEA这么高端的工具之前只是断断续续使用了一下,因为项目的开发都是在eclipse上,每次学习IDEA的使用都得上网搜索半天,今天自己整理一下,方便以后查阅. IDEA版本15.0.4 字体 界面字 ...
- hdu - 2645 find the nearest station (bfs水)
http://acm.hdu.edu.cn/showproblem.php?pid=2645 找出每个点到距离最近的车站的距离. 直接bfs就好. #include <cstdio> #i ...
- Maximum Product Subarray(最大连续乘积子序列)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- ubuntu忘记root密码的解决办法
ubuntu忘记密码,不需要重装系统即可重新设置root密码,以下是步骤: 1)在系统一启动时,按ESC键,目的是为了出现选单页面 2) 当看到选单页面时,此时按下[e] 这个键,此时会进入grub ...
- 在springBoot与quartz 整合中 @Transaction 失效
问题1::springBoot在与quartz 整合时,使用@Transaction 注解时事务失效 解决方案:创建一个类使用@component被spring管理 ,使用@Transaction标识 ...
- 【剑指Offer面试题】 九度OJ1385:重建二叉树
题目链接地址: pid=1385">http://ac.jobdu.com/problem.php?pid=1385 题目1385:重建二叉树 时间限制:1 秒内存限制:32 兆特殊判 ...
- LinQ开篇介绍
语言集成查询(LINQ)是 Visual Studio2008中引入的一组功能. 可为 C# 和 Visual Basic 语言语法提供强大的查询功能. LINQ引入了标准易学的数据查询和更新模式,能 ...
- 【C++ STL应用与实现】18: 怎样使用迭代器适配器
本系列文章的文件夹在这里:文件夹. 通过文件夹里能够对STL整体有个大概了解 前言 本文介绍了STL中的迭代器适配器(iterator adapter)的概念及其用法演示样例.迭代器适配器能够和标准库 ...
- NYOJ 55 懒省事的小明(哈弗曼树)
懒省事的小明 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描写叙述 小明非常想吃果子,正好果园果子熟了. 在果园里,小明已经将全部的果子打了下来,并且按果子的不 ...
- node-load module
javscript :脚本建共享全局名称空间(全局污染). node:实现CommonJS(公共)模块标准. Node加载模块,有两种方式: 1.通过名称 除非是核心模块,否则被引用的模块最后都会映射 ...