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.

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模板题/哈希模板题的更多相关文章

  1. YTU 2642: 填空题:类模板---求数组的最大值

    2642: 填空题:类模板---求数组的最大值 时间限制: 1 Sec  内存限制: 128 MB 提交: 646  解决: 446 题目描述   类模板---求数组的最大值    找出一个数组中的元 ...

  2. KMP算法,匹配字符串模板(返回下标)

    //KMP算法,匹配字符串模板 void getNext(int[] next, String t) { int n = next.length; for (int i = 1, j = 0; i & ...

  3. freeMarker(四)——模板开发指南之模板

    学习笔记,选自freeMarker中文文档,译自 Email: ddekany at users.sourceforge.net 模板开发指南之模板 1. 总体结构 实际上用程序语言编写的程序就是模板 ...

  4. Handlebars的基本用法 Handlebars.js使用介绍 http://handlebarsjs.com/ Handlebars.js 模板引擎 javascript/jquery模板引擎——Handlebars初体验 handlebars.js 入门(1) 作为一名前端的你,必须掌握的模板引擎:Handlebars 前端数据模板handlebars与jquery整

    Handlebars的基本用法 使用Handlebars,你可以轻松创建语义化模板,Mustache模板和Handlebars是兼容的,所以你可以将Mustache导入Handlebars以使用 Ha ...

  5. JS 模板引擎之JST模板

    项目中有用到JST模板引擎,于是抽个时间出来,整理了下关于JST模板引擎的相关内容. 试想一个场景,当点击页面上列表的翻页按钮后,通过异步请求获得下一页的列表数据并在页面上显示出来.传统的JS做法是编 ...

  6. 30余套系统模版|DIV+CSS网页模板|简洁大气系统模板

    30余套系统模版|DIV+CSS网页模板|简洁大气系统模板.都是一些后台系统的模版,很适合开发一些管理系统,办公系统,网站后台系统等.使用很广泛,很实用的系统模版. 下载地址: 点击下载

  7. 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 ...

  8. C++模板学习:函数模板、结构体模板、类模板

    C++模板:函数.结构体.类 模板实现 1.前言:(知道有模板这回事的童鞋请忽视) 普通函数.函数重载.模板函数 认识. //学过c的童鞋们一定都写过函数sum吧,当时是这样写的: int sum(i ...

  9. 模板类的约束模板友元函数:template friend functions

    本来这篇博客是不打算写的,内容不是很难,对于我自己来讲,更多的是为了突出细节. 所谓template friend functions,就是使友元函数本身成为模板.基本步骤:1,在类定义的前面声明每个 ...

随机推荐

  1. shell 例子

    shell编程入门 http://www.runoob.com/linux/linux-shell-variable.html http://c.biancheng.net/cpp/shell/ .查 ...

  2. 2 java程序入门

    1. 第一个java  class  { public static void main(String[] args) { System.out.println("Hello World!& ...

  3. Redis探索之路(六):Redis的常用命令

    一:键值相关命令 1.keys Pattern模糊查询 keys my* 2.exists某个key是否存在 exists key1 3.del 删除一个key del key1 4.expire设置 ...

  4. linux秘钥登录

    秘钥登录首先要了解四个文件: 公钥文件,私钥文件, authorized_keys, 还有/etc/ssh/sshd_config配置文件. 公钥文件存放在被登陆的机器上, 要将公钥添加进author ...

  5. delphi 文件存取方法与文件管理组件

    9.2  文件存取方法与文件管理组件 9.2.1  存取文件的类方法 Delphi在许多需要与文件打交道的类中定义了文件存取方法,使用这些方法可以非常方便地将类中的数据保存到文件中,或从文件中读取所需 ...

  6. HTML5表格(table)篇

    初学HTML接触table少不了,但是实际应用的地方也有. 简单说明HTML <table> 标签 定义和用法 <table> 标签定义 HTML 表格. 简单的 HTML 表 ...

  7. NX二次开发-UFUN CSYS坐标系转换UF_CSYS_map_point

    1 NX9+VS2012 2 3 #include <uf.h> 4 #include <uf_curve.h> 5 #include <uf_csys.h> 6 ...

  8. getmapping等无法解析

    版本要改一下,4.1.6没有<dependency> <groupId>org.springframework</groupId> <artifactId&g ...

  9. HDU 5531

    题目大意: 给定一个n边形的顶点 以每个顶点为圆心画圆(半径可为0) 每个顶点的圆要和它相邻顶点的圆相切(不相邻的可相交) 求所有圆的最小面积总和并给出所有圆的半径 设半径为r1 r2 ... rn, ...

  10. Number Sequence /// oj21456

    题目大意: 有一组规律数 the first 80 digits of the sequence are as follows: 1 12 123 1234 12345 123456 1234567 ...