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,在类定义的前面声明每个 ...
随机推荐
- 安装和使用pyspider框架时遇到的问题
安装pyspider, 直接cmd中输入 pip install pyspider, 而后提示报错 从网上找到解决方法,pycurl需要根据python版本采用wheel方法安装. 下载链接为http ...
- NX二次开发-UF_MODL_ask_point_containment获取一个点是在体(面,边)的边界内部,外部,还是边界上
NX9+VS2012 #include <uf.h> #include <uf_modl.h> #include <uf_curve.h> #include < ...
- 牛客多校第十场 H Stammering Chemists 判断图同构
题意: 给出一个无向图,表示一种有机物质的结构式,问你这个有机物质是列表中的哪个. 题解: 判断图同构需要枚举全排列以对应点,但是此题中几乎只需要将点度数排序后一个一个比较,对于甲基位置再加个特判即可 ...
- hdu多校第五场1007 (hdu6630) permutation 2 dp
题意: 给你n个数,求如下限制条件下的排列数:1,第一位必须是x,2,最后一位必须是y,3,相邻两位之差小于等于2 题解: 如果x<y,那么考虑把整个数列翻转过来,减少讨论分支. 设dp[n]为 ...
- mac 安装配置使用 mongoldb
mac 安装配置使用 mongoldb 安装和配置 brew install mongos brew install mongo # 密码就是用户的密码 # 配置数据文件 //如果不配置会出现错误62 ...
- day 87 DjangoRestFramework学习一之restful规范、APIview、解析器组件、Postman等
DjangoRestFramework学习一之restful规范.APIview.解析器组件.Postman等 本节目录 一 预备知识 二 restful规范 三 DRF的APIView和解析器组 ...
- 如何优雅的使用Objects.requireNonNull(T obj, String message)定制你的NPE异常
IDEA中习惯跟踪源码实现逻辑,多次碰到Objects.requireNonNull(T obj)这个方法,改方法主要用于提早判断对象是否为空,以便更早的抛出NPE 平时小组开发中强调程序健壮性,不允 ...
- spark自定义函数之——UDF使用详解及代码示例
前言 本文介绍如何在Spark Sql和DataFrame中使用UDF,如何利用UDF给一个表或者一个DataFrame根据需求添加几列,并给出了旧版(Spark1.x)和新版(Spark2.x)完整 ...
- 02ubuntu下python环境安装
原文链接:https://blog.csdn.net/weixin_42549407/article/details/85198460 我安装的是python3.6.9 1.下载python的源码压缩 ...
- 通过Python SDK 获取tushare数据
导入tushare import tushare as ts 这里注意, tushare版本需大于1.2.10 设置token ts.set_token('your token here') 以上方法 ...

