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 ...
随机推荐
- .net 4.0 : Missing compiler required member 'Microsoft.CSharp.RuntimeBinder.Binder.****'
解决办法,添加 MicroSoft.CSharp 的引用.
- SpringBoot-属性配置yaml自定义属性和值
SpringBoot-属性配置yaml自定义属性和值 SpringBoot-属性配置yaml自定义属性和值 在SpringBoot中yml/yaml文件可以自定义一些属性,以供注入给自定义bean对象 ...
- JAVA实现数组的反转--基础
直接上代码 这个算法比较简单,唯一需要注意的就是第8行和第9行.一定要多减去1 因为for循环从0开始,而数组长度是从0到length-1的. class ArrReverse { //实现数组元素的 ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-download
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- POJ 3614:Sunscreen 贪心+优先队列
Sunscreen Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5378 Accepted: 1864 Descrip ...
- maven-本地安装jar包
maven 安装本地jar包,通过install插件的install-file mojo进行工作,具体可通过如下命令进行查看 mvn help:describe -Dplugin=install -D ...
- Django——优美的Path()函数
path( )作用:解析URL地址 path( ) 标准语法: (<>为必须的参数,[]为可选参数) path(<route>, <view>, [name=Non ...
- 016-PHP读取文件常见属性
<?php print("文件的所有者(UID 值):"); print(fileowner("data.txt") . "<br> ...
- 132-PHP子类和父类同名函数的调用
<?php class father{ //定义father类 public function cook(){ return '烹饪'; } } class son extends father ...
- 060-PHP函数定义和调用
<?php function add($x,$y){ //定义函数add return $x+$y; } echo add(15,6); //调用函数并输出结果 ?>