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. 使用js 判断当前运行环境实在浏览器还是在手机

    转: 在跨平台,各种浏览器,移动设备兼容的时候,经常要根据设备.浏览器做特定调整,所以判断设备和浏览器的工作,经常会用到,这里做一下总结. 有关浏览器类型的信息都藏在USER-AGENT里面,首先读取 ...

  2. Python加密模块

    RSA加密 # 生成公钥私钥对象 import rsa pub_key_obj, priv_key_obj = rsa.newkeys(1024) ''' 这里的1024是二进制位数, 也就是说他加密 ...

  3. Android SDK 环境变量配置

    ANDROID_HOME = D:\Package\android-sdk-windows 在path 中加入 %ANDROID_HOME%\tools 和 %ANDROID_HOME%\platfo ...

  4. Android中的Service详解

    今天我们就来介绍一下Android中的四大组件中的服务Service,说到Service, 它分为本地服务和远程服务:区分这两种服务就是看客户端和服务端是否在同一个进程中,本地服务是在同一进程中的,远 ...

  5. paper 15 :整理的CV代码合集

    这篇blog,原来是西弗吉利亚大学的Li xin整理的,CV代码相当的全,不知道要经过多长时间的积累才会有这么丰富的资源,在此谢谢LI Xin .我现在分享给大家,希望可以共同进步!还有,我需要说一下 ...

  6. NX二次开发-UFUN获取当前所在的模块UF_ask_application_module

    NX9+VS2012 #include <uf.h> #include <NXOpen/UI.hxx> #include <NXOpen/MenuBar_MenuBarM ...

  7. sql语句中----删除表数据drop、truncate和delete的用法(转)

    转载于:http://www.cr173.com/html/40708_1.html 说到删除表数据的关键字,大家记得最多的可能就是delete了 然而我们做数据库开发,读取数据库数据.对另外的两兄弟 ...

  8. Java父类强制转换子类原则

    最近,微信群友在讨论子类父类的转换问题,其实不难,给大家用实例来说明一下就很明了了. 我们知道Java中子类转换成父类是没有任何问题的,那父类可以转换成子类吗? 来看下面这段程序: public cl ...

  9. 根据单个或多个字段对list对象去重

    pojo  省略 在list 对象中,根据某一字段进行去重,重写Comparator /** * 去重 * * @param orderList * @return * @author ziggo * ...

  10. PyTorch中,关于model.eval()和torch.no_grad()

    一直对于model.eval()和torch.no_grad()有些疑惑 之前看博客说,只用torch.no_grad()即可 但是今天查资料,发现不是这样,而是两者都用,因为两者有着不同的作用 引用 ...