POJ 1200 Crazy Search 字符串的Hash查找
2024-08-24 10:42:09
第一次涉及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);
}
}
最新文章
- 让kindeditor显示高亮代码
- 17.Java 反射机制
- less文件编译成微信小程序wxss文件
- CQRS\ES架构介绍
- (译)详解javascript立即执行函数表达式(IIFE)
- 循序渐进Python3(三) -- 1 -- 内置函数
- HDU 4883 TIANKENG’s restaurant
- stm32定义GPIO口方向和操作的代码
- CSS width:100%和width:auto的区别
- cocos2d-x中CCCallFunc CCCallFuncN CCCallFuncND的区别和使用示例
- HTML5学习资源
- mysql if then
- SQL之case when then用法(用于分类统计)
- 安装wamp环境 最新完整版
- 带查询参数 可分页 的 T-SQL 语句写法
- git merge 步骤
- css居中小结
- 解决urbuntu桌面本客户端输入ll command not found
- FFMPEG采集摄像头数据并切片为iPhone的HTTP Stream流
- NGUI实现简单的倒计时组件
热门文章
- JAVA String类常用方法
- 吴裕雄--天生自然java开发常用类库学习笔记:RumTime类
- Docker入门以及漏洞环境搭建(10.23 第二十五天)
- c#查看本机网络端口和对应的程序名
- idea开发springboot 的mysql数据库连接问题
- 洛谷 P1934 封印
- require(): open_basedir restriction in effect. File(/www/wwwroot/xcx/zerg/thinkphp/start.php) is not within the allowed path(s): (/www/wwwroot/xcx/zerg/public/:/tmp/:/proc/) in /www/wwwroot/xcx/zerg/p
- 二十六、JavaScript之查找子字符串substring和slice和substr
- Reference在Essay写作中的最佳占比是多少?
- nui UI 具有右键属性的菜单树