第一次涉及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);
}
}

最新文章

  1. 让kindeditor显示高亮代码
  2. 17.Java 反射机制
  3. less文件编译成微信小程序wxss文件
  4. CQRS\ES架构介绍
  5. (译)详解javascript立即执行函数表达式(IIFE)
  6. 循序渐进Python3(三) -- 1 -- 内置函数
  7. HDU 4883 TIANKENG’s restaurant
  8. stm32定义GPIO口方向和操作的代码
  9. CSS width:100%和width:auto的区别
  10. cocos2d-x中CCCallFunc CCCallFuncN CCCallFuncND的区别和使用示例
  11. HTML5学习资源
  12. mysql if then
  13. SQL之case when then用法(用于分类统计)
  14. 安装wamp环境 最新完整版
  15. 带查询参数 可分页 的 T-SQL 语句写法
  16. git merge 步骤
  17. css居中小结
  18. 解决urbuntu桌面本客户端输入ll command not found
  19. FFMPEG采集摄像头数据并切片为iPhone的HTTP Stream流
  20. NGUI实现简单的倒计时组件

热门文章

  1. JAVA String类常用方法
  2. 吴裕雄--天生自然java开发常用类库学习笔记:RumTime类
  3. Docker入门以及漏洞环境搭建(10.23 第二十五天)
  4. c#查看本机网络端口和对应的程序名
  5. idea开发springboot 的mysql数据库连接问题
  6. 洛谷 P1934 封印
  7. 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
  8. 二十六、JavaScript之查找子字符串substring和slice和substr
  9. Reference在Essay写作中的最佳占比是多少?
  10. nui UI 具有右键属性的菜单树