HUD1686-Oulipo-kmp模板题/哈希模板题
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.
InputThe 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.
OutputFor 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 题意:计算模式串在原串中出现的次数 对于next数组的理解是理解kmp的关键。
next[i]:记录的是前后缀最长公共长度。
KMP:
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std; const int N=;
const int M=; char s[N];//原串
char t[M];//模式串
int nextt[M]; void getnext(int len)//求的是模式串的next数组
{
int i=,j=-;
nextt[]=-;
while(i<len)
{
if(j<||t[i]==t[j])
nextt[++i]=++j;
else
j=nextt[j];
}
} int kmp(int m,int n)//m模式串长度、n原串长度
{
int i=,j=,ans=;
while(i<n)
{
if(j==-||t[j]==s[i])
{
i++;
j++;
}
else
j=nextt[j];
if(j==m)
{
ans++;
j=nextt[j];
}
}
return ans;
} int main()
{
int tt;
scanf("%d",&tt);
while(tt--)
{
memset(s,'\0',sizeof(s));
memset(t,'\0',sizeof(t));
memset(nextt,,sizeof(nextt));
scanf("%s%s",t,s);//模式串、原串
int len1=strlen(t);//模式串
int len2=strlen(s);//原串
getnext(len1);
printf("%d\n",kmp(len1,len2));
}
return ;
}
哈希:

#include<stdio.h>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<string.h>
#include<queue>
#include<map>
using namespace std;
typedef unsigned long long ull;
const int N=1e6+; char a[N],b[N];
ull p[N],sum[N],x=;
//求a(子串)在b(母串)中出现多少次 void w()
{
p[]=;
for(int i=; i<; i++)
p[i]=p[i-]*x;//预处理出x^n
} int main()
{
w();
int t;
scanf("%d",&t);
while(t--)
{
scanf("%s %s",a+,b+);//使得下标从1开始
int la=strlen(a+);//短
int lb=strlen(b+);//长
sum[]=;
for(int i=; i<=lb; i++)
sum[i]=sum[i-]*x+(ull)(b[i]-'A'+);
ull s=;
for(int i=; i<=la; i++)
s=s*x+(ull)(a[i]-'A'+);//*x是为了化成x进制数
int ans=;
for(int i=; i<=lb-la; i++)
{
if(s==sum[i+la]-sum[i]*p[la])
ans++;
}
printf("%d\n",ans);
}
return ;
}
不明白为什么哈希的时间比kmp慢而且占用的内存都快是kmp的10倍了???
HUD1686-Oulipo-kmp模板题/哈希模板题的更多相关文章
- YTU 2642: 填空题:类模板---求数组的最大值
2642: 填空题:类模板---求数组的最大值 时间限制: 1 Sec 内存限制: 128 MB 提交: 646 解决: 446 题目描述 类模板---求数组的最大值 找出一个数组中的元 ...
- KMP算法,匹配字符串模板(返回下标)
//KMP算法,匹配字符串模板 void getNext(int[] next, String t) { int n = next.length; for (int i = 1, j = 0; i & ...
- freeMarker(四)——模板开发指南之模板
学习笔记,选自freeMarker中文文档,译自 Email: ddekany at users.sourceforge.net 模板开发指南之模板 1. 总体结构 实际上用程序语言编写的程序就是模板 ...
- Handlebars的基本用法 Handlebars.js使用介绍 http://handlebarsjs.com/ Handlebars.js 模板引擎 javascript/jquery模板引擎——Handlebars初体验 handlebars.js 入门(1) 作为一名前端的你,必须掌握的模板引擎:Handlebars 前端数据模板handlebars与jquery整
Handlebars的基本用法 使用Handlebars,你可以轻松创建语义化模板,Mustache模板和Handlebars是兼容的,所以你可以将Mustache导入Handlebars以使用 Ha ...
- JS 模板引擎之JST模板
项目中有用到JST模板引擎,于是抽个时间出来,整理了下关于JST模板引擎的相关内容. 试想一个场景,当点击页面上列表的翻页按钮后,通过异步请求获得下一页的列表数据并在页面上显示出来.传统的JS做法是编 ...
- 30余套系统模版|DIV+CSS网页模板|简洁大气系统模板
30余套系统模版|DIV+CSS网页模板|简洁大气系统模板.都是一些后台系统的模版,很适合开发一些管理系统,办公系统,网站后台系统等.使用很广泛,很实用的系统模版. 下载地址: 点击下载
- vs 2013下自定义ASP.net MVC 5/Web API 2 模板(T4 视图模板/控制器模板)
vs 2013下自定义ASP.net MVC 5/Web API 2 模板(T4 视图模板/控制器模板): Customizing ASP.NET MVC 5/Web API 2 Scaffoldi ...
- C++模板学习:函数模板、结构体模板、类模板
C++模板:函数.结构体.类 模板实现 1.前言:(知道有模板这回事的童鞋请忽视) 普通函数.函数重载.模板函数 认识. //学过c的童鞋们一定都写过函数sum吧,当时是这样写的: int sum(i ...
- 模板类的约束模板友元函数:template friend functions
本来这篇博客是不打算写的,内容不是很难,对于我自己来讲,更多的是为了突出细节. 所谓template friend functions,就是使友元函数本身成为模板.基本步骤:1,在类定义的前面声明每个 ...
随机推荐
- 2017-3-7html基础
一:<html><!--html文档的开始--> <head><!--头标签,html文档的开头,来描述html文档的信息,head之间的内容不会在浏览器中显 ...
- thinkphp环境要求
框架本身没有什么特别模块要求,具体的应用系统运行环境要求视开发所涉及的模块.ThinkPHP底层运行的内存消耗极低,而本身的文件大小也是轻量级的,因此不会出现空间和内存占用的瓶颈. PHP版本要求 P ...
- 欧拉函数+反演——2019hdu多校6588
\[ 求\sum_{i=1}^{n}(\sqrt[3]i,i)\\ 首先转化一下这个式子,考虑对于i\in[j^3,(j+1)^3-1],\sqrt[3]i=j\\ 所以可以枚举所有j,然后对i\in ...
- 优化剪枝搜索——牛客多校第二场F
试了很多种爆搜和剪枝,最后发现还是状压的比较好用 #include <bits/stdc++.h> using namespace std; // #define IO #define f ...
- 树状数组优化dp——cf985E经典
/* dp[i]=0|1 表示前i是否可以被成功分组, dp[i+1]能够变成1的三个条件 1:j∈[0,i-k]里存在dp[j]=1 2:dp[j]=1 3:a[i+1]-a[j+1]<=d ...
- (转)mysql分区技术2
转:http://database.51cto.com/art/201002/184392.htm 非整数列分区 任何使用过分区的人应该都遇到过不少问题,特别是面对非整数列分区时,MySQL 5.1只 ...
- 李宏毅机器学习课程---2、Regression - Case Study
李宏毅机器学习课程---2.Regression - Case Study 一.总结 一句话总结: 分类讨论可能是比较好的找最佳函数的方法:如果 有这样的因素存在的话 模型不够好,可能是因素没有找全 ...
- pycharm 使用心得(一)安装和首次使用
PyCharm 是我用过的python编辑器中,比较顺手的一个.而且可以跨平台,在macos和windows下面都可以用,这点比较好. 首先预览一下 PyCharm 在实际应用中的界面:(更改了PyC ...
- 修改linux默认语言
linux修改默认语言 编辑/etc/sysconfig/i18n这个文件(不存在就新建一个),原内容如下: LANG="en_US.UTF-8" SYSFONT=" ...
- hexo next主题深度优化(五),评论系统换成gittalk
文章目录 背景: 开始: 新建comments_git.js 找到comments.swig在最后一个endif之前 引入代码 pjax加入gitalk 遇到的问题 所有的页面共享的一个评论issue ...

