POJ 1200 Crazy Search 字符串的Hash查找
第一次涉及HASH查找的知识
对于字符串的查找有很多前人开发出来的HASH函数,比较常用的好像是ELF 和 BKDR。
这道题没想到突破点是在于其nc值,告诉你组成字符串的字母种类。
还有用26进制,不管怎么说,只要避免产生冲突,怎么哈希都行。
用的是BKDRHash法。
#include <iostream>
#include <cstdio>
#include <cstring>
#define maxn 20000000
#define mm 1000000
using namespace std;
int hash[maxn]={};
char str[mm];
int news[mm]={}; int ans;
int n,nc; int BKDRHash(char *key)
{
int seed=nc;
int p=;
//cout<<"pass";
while (*key)
{
p=p*seed+(news[(*key++)-'a']);
}
return (p & 0x7FFFFFFF);
}
void hashit (char *s)
{
//memset(hash,0,sizeof hash);
int i,j,k;
char ch[mm]; for (i=;s[i+n-];i++)
{
for (j=;j<n;j++)
ch[j]=s[i+j];
ch[j]='\0';
//puts(ch);
int k=BKDRHash(ch);
k%=maxn;
//cout<<k<<endl;
if (!hash[k])
{
ans++;
hash[k]=;
//cout<<"pass"<<endl;
} }
}
int main()
{ while (scanf("%d %d",&n,&nc)!=EOF)
{
ans=;
getchar();
gets(str);
int cnt=;
int i,j;
for (i=;str[i];i++)
{
if (news[str[i]-'a']==) //使用二十六进制
news[str[i]-'a']=cnt++;
}
hashit(str);
printf("%d\n",ans);
}
}
POJ 1200 Crazy Search 字符串的Hash查找的更多相关文章
- POJ 1200 Crazy Search(字符串简单的hash)
题目:http://poj.org/problem?id=1200 最近看了一个关于hash的问题,不是很明白,于是乎就找了些关于这方面的题目,这道题是一道简单的hash 字符串题目,就先从他入手吧. ...
- poj 1200 Crazy Search(hash)
题目链接:http://poj.org/problem?id=1200 思路分析:从数据来看,该题目使用线性时间算法,可见子串的比较是不可能的:使用hash可以在常数时间内查找,可以常数时间内判重, ...
- POJ 1200 Crazy Search 【hash】
<题目链接> 题目大意: 给定n,nc,和一个字符串,该字符串由nc种字符组成,现在要你寻找该字符串中长度为n的子字符串有多少种. 解题分析: 因为要判重,所以讲这些字符串hash一下,将 ...
- POJ 1200 Crazy Search【Hash入门】
RK法:https://www.cnblogs.com/16crow/p/6879988.html #include<cstdio> #include<string> #inc ...
- POJ 1200 Crazy Search (哈希)
题目链接 Description Many people like to solve hard puzzles some of which may lead them to madness. One ...
- POJ – 1200 Crazy Search
http://poj.org/problem?id=1200 #include<iostream> #include<cstring> using namespace std; ...
- POJ 1200 Crazy Search
思路:利用Karp-Rabin算法的思想,对每个子串进行Hash,如果Hash值相等则认为这两个子串是相同的(事实上还需要做进一步检查),Karp-Rabin算法的Hash函数有多种形式,但思想都是把 ...
- poj 1200 crasy search
https://vjudge.net/problem/POJ-1200 题意: 给出一个字符串,给出子串的长度n和给出的字符串中不同字符的个数nc,统计这个字符串一共有多少不同的长度为n的子串. 思路 ...
- POJ 1200:Crazy Search(哈希)
Crazy Search Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 32483 Accepted: 8947 Des ...
随机推荐
- pig安装配置及实例
一.前提 1. hadoop集群环境配置好(本人hadoop版本:hadoop-2.7.3) 2. windows基础环境准备: jdk环境配置.esclipse环境配置 二.搭建pig环境 1.下载 ...
- POJ 2829 Buy Tickets
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 26443 Accepted: 12639 Des ...
- C++ Winsock
由于兼容的问题更新下winsock,有较好的移植性:客户端是非阻塞的,服务器是阻塞的! Win32控制台: 数据收发: 服务器向客户端发送一个txt文本内容和一个结构体数据: 服务器代码: #incl ...
- Delphi MD5
unit uMD5; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics; type MD5Count = ...
- 201912-1 报数 Java
思路: String.valueOf(int i) : 将 int 变量 i 转换成字符串 String.contains()用于判断字符串是否包含子字符串 import java.util.Scan ...
- Apache部署Django+Vue
首先部署Vue,后端项目django开5000端口,所以vue里的路由是ip:5000,然后打包npm run build 生成dist文件 把dist文件里的index.html和static放在/ ...
- Sklearn 速查
## 版权所有,转帖注明出处 章节 SciKit-Learn 加载数据集 SciKit-Learn 数据集基本信息 SciKit-Learn 使用matplotlib可视化数据 SciKit-Lear ...
- B. The Number of Products(Codeforces Round #585 (Div. 2))
本题地址: https://codeforces.com/contest/1215/problem/B 本场比赛A题题解:https://www.cnblogs.com/liyexin/p/11535 ...
- input内容,输入账号密码
在爬取需要输入账号密码的网页时,我们需要找到可填写内容的标签.记得之前写过,但是没有记住. 这回重新梳理一下,如何可找到这个标签 以极验后台登录网站为例:https://auth.geetest.co ...
- 安装VMtools vim编辑器的使用 压缩包命令 Linux下的用户管理 (第三天)
VM tools:方便我们虚拟机和宿主机之间复制数据或移动文件等 安装VMtools 1.菜单栏-虚拟机-安装VM tools 将其选中 2.进入系统,在桌面位置里面有VM tools的光盘,双击进入 ...