HDU1686

题意:

找A串在B串中的出现次数(可重叠),可用KMP做,这里只提供哈希算法做的方法

题解:

先得到A串的hash值,然后在B中枚举起点,长度为lena的子串,检验hash值是否相同就可以了。

代码:

/*
-1 在ull里相当于2的六四次-2
ull炸了就当mod2e64
*/ #include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
typedef unsigned long long ull;
const int blo=31;
const int maxn = 1e6+5;
ull xp[maxn],hash_1[maxn],hash_2[maxn];
void init()
{
xp[0]=1;
for(int i=1; i<maxn; i++)
xp[i]=xp[i-1]*blo;
}
ull get_hash(int i,int L,ull hash_[])//get_hash(i,L)可以得到从位置i开始的,长度为L的子串的hash值.
{
return hash_[i]-hash_[i+L]*xp[L];
}
int make_hash(char str[],ull hash_[])
{
int len=strlen(str);
hash_[len]=0;
for(int i=len-1; i>=0; i--)
{
hash_[i]=hash_[i+1]*blo+(str[i]-'A'+1);
//cout<<hash_[i]<<" ";
}
return len;
}
char str[maxn],str2[maxn];
int main()
{ init();
//printf("%d\n",31644418079342855-13357*xp[3]);
int t;
scanf("%d",&t);
while(t--)
{
int ans=0;
scanf("%s%s",str,str2);
int len1=make_hash(str,hash_1);
int len2=make_hash(str2,hash_2);
//printf("\n");
ull tmp=get_hash(0,len1,hash_1);
//cout<<tmp<<"****"<<xp[len1]<<endl;
for(int i=0; i<len2-len1+1; i++) //注意枚举时的边界问题
{
if(get_hash(i,len1,hash_2)==tmp)
ans++;
}
printf("%d\n",ans);
}
return 0;
}

POJ2774

题意:

求两个串的最长公共子串,另一种解法是后缀数组,这里只讲哈希算法求解过程

题解:

由于没有给定长度,要求长度,这时就要想是否具有二分的性质,发现答案是具有二分性质的,所以我们可以二分答案,然后把A串中所有出现过的hash值放进一个数组,sort一下,然后对于每个B串产生的hash用lower_bound查询是否出现过,若出现过则直接返回true.复杂度是o(len*log(len)*log(len))o(len∗log(len)∗log(len))。不能使用map,因为map的复杂度要多一个log

代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<map>
using namespace std;
const int maxn=1e6+10;
const int blo=31;
typedef unsigned long long ull;
ull xp[maxn],hash_1[maxn],hash_2[maxn];
ull a[maxn];
int len1,len2;
void init()
{
xp[0]=1;
for(int i=1;i<maxn;++i)
{
xp[i]=xp[i-1]*blo;
}
}
ull Get_hash(int i,int len,ull hash_[])
{
return hash_[i]-hash_[i+len]*xp[len];
}
int Make_hash(char s[],ull hash_[])
{
int len=strlen(s);
hash_[len]=0;
for(int i=len-1;i>=0;--i)
{
hash_[i]=hash_[i+1]*blo+(s[i]-'A'+1);
}
return len;
}
char s1[maxn],s2[maxn];
int check(int x)
{
int cnt=0;
for(int i=0;i<len1-x+1;i++)
{
a[cnt++]=Get_hash(i,x,hash_1);
}
sort(a,a+cnt);
for(int i=0;i<len2-x+1;++i)
{
ull tmp=Get_hash(i,x,hash_2);
int pos=lower_bound(a,a+cnt,tmp)-a;
if(a[pos]==tmp) return 1;
}
return 0;
}
int main()
{
init();
while(~scanf("%s%s",s1,s2))
{
len1=Make_hash(s1,hash_1);
len2=Make_hash(s2,hash_2);
int l=0,r=min(len1,len2),mid,ans=0;
while(l<=r)
{
mid=(l+r)>>1;
if(check(mid))
{
ans=mid;
l=mid+1;
}
else r=mid-1;
}
printf("%d\n",ans);
}
return 0;
}

POJ3261

题意:

求字符串中至少出现过k次的最长子串。

题解:

这道题哈希做法和上一道题很相似,你只需要确定一个这个串的出现次数大于等于k,那么可以在用一个upper_bound()函数和一个lower_bound()来确定这个子串在主串中的出现次数

代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<map>
using namespace std;
const int maxn=2e4+10;
const int blo=31;
typedef unsigned long long ull;
ull xp[maxn],hash_1[maxn];
ull a[maxn];
int n,k;
void init()
{
xp[0]=1;
for(int i=1;i<maxn;++i)
{
xp[i]=xp[i-1]*blo;
}
}
ull Get_hash(int i,int len,ull hash_[])
{
return hash_[i]-hash_[i+len]*xp[len];
}
void Make_hash(ull s[],ull hash_[])
{
hash_[n]=0;
for(int i=n-1;i>=0;--i)
{
hash_[i]=hash_[i+1]*blo+s[i];
}
//return len;
}
ull s[maxn];
int check(int x)
{
int cnt=0;
for(int i=0;i<n-x+1;i++)
{
a[cnt++]=Get_hash(i,x,hash_1);
}
sort(a,a+cnt);
for(int i=0;i<n-x+1;++i)
{
ull tmp=Get_hash(i,x,hash_1);
int pos=lower_bound(a,a+cnt,tmp)-a;
if(a[pos]==tmp)
{
int index=upper_bound(a,a+cnt,tmp)-a;
//printf("%d %d %d %d\n",pos,index,i,x);
if(index-pos>=k) //这里是大于等于号
return 1;
//else return 0;
}
}
return 0;
}
int main()
{ init();
while(~scanf("%d%d",&n,&k))
{
for(int i=0;i<n;++i)
scanf("%llu",&s[i]);
Make_hash(s,hash_1);
int l=0,r=n,mid,ans=0;
while(l<=r)
{
mid=(l+r)>>1;
if(check(mid))
{
ans=mid;
l=mid+1;
}
else r=mid-1;
}
printf("%d\n",ans);
}
return 0;
}

哈希算法解决:HDU1686 && POJ2774 && POJ3261的更多相关文章

  1. 一致性哈希算法-----> 解决memecache 服务器扩容后的数据丢失。

    1 基本场景 比如你有 N 个 cache 服务器(后面简称 cache ),那么如何将一个对象 object 映射到 N 个 cache 上呢,你很可能会采用类似下面的通用方法计算 object 的 ...

  2. 一致性哈希算法(Consistent Hashing) .

    应用场景 这里我先描述一个极其简单的业务场景:用4台Cache服务器缓存所有Object. 那么我将如何把一个Object映射至对应的Cache服务器呢?最简单的方法设置缓存规则:object.has ...

  3. 一致性哈希算法——算法解决的核心问题是当slot数发生变化时,能够尽量少的移动数据

    一致性哈希算法 摘自:http://blog.codinglabs.org/articles/consistent-hashing.html 算法简述 一致性哈希算法(Consistent Hashi ...

  4. _00013 一致性哈希算法 Consistent Hashing 新的讨论,并出现相应的解决

    笔者博文:妳那伊抹微笑 博客地址:http://blog.csdn.net/u012185296 个性签名:世界上最遥远的距离不是天涯,也不是海角,而是我站在妳的面前.妳却感觉不到我的存在 技术方向: ...

  5. R语言实现︱局部敏感哈希算法(LSH)解决文本机械相似性的问题(二,textreuse介绍)

    每每以为攀得众山小,可.每每又切实来到起点,大牛们,缓缓脚步来俺笔记葩分享一下吧,please~ --------------------------- 上一篇(R语言实现︱局部敏感哈希算法(LSH) ...

  6. 用哈希算法的思想解决排序和字符串去重问题,时间复杂度为O(N)

    第一个题目: int a[] = {12,13,12,13,19,18,15,12,15,16,17},要求对数组a进行排序,要求时间复杂度为O(N) 我们所知道的常规排序中,最优的解法也就是O(N* ...

  7. 五分钟理解一致性哈希算法(consistent hashing)

    转载请说明出处:http://blog.csdn.net/cywosp/article/details/23397179 一致性哈希算法在1997年由麻省理工学院提出的一种分布式哈希(DHT)实现算法 ...

  8. 每天进步一点点——五分钟理解一致性哈希算法(consistent hashing)

    转载请说明出处:http://blog.csdn.net/cywosp/article/details/23397179     一致性哈希算法在1997年由麻省理工学院提出的一种分布式哈希(DHT) ...

  9. 一致性哈希算法以及其PHP实现

    在做服务器负载均衡时候可供选择的负载均衡的算法有很多,包括:  轮循算法(Round Robin).哈希算法(HASH).最少连接算法(Least Connection).响应速度算法(Respons ...

随机推荐

  1. Selenium WebDriver 定位之Xpath定位

    Selenium 定位之Xpath定位: 1.绝对路径定位:以/开头从根节点一直找到当前节点,不推荐使用决定路径定位方式 2.相对路径定位:使用"//"表示相对路径定位,格式:// ...

  2. Deep Learn I'm back.

    Intorduction: 时隔好几个月,我准备重新进入Deep Learning 的领域.昨天和老师聊了很多,之前觉得我做的工作就是排列组合,在水论文,灌水.但老师却说:这也是为将来的研究打基础. ...

  3. Linux 使用命令行上传下载文件

    基本语法: 服务器: 用户名@ip:/路径 scp 要拷贝的文件 要存放的文件 上传文件到服务器 # 把本地 source.md 文件上传到 152.116.113.13 服务器的/home目录 # ...

  4. Error: Could not request certificate: No route to host - connect(2)

    [root@puppetclient ~]# puppet agent --server 192.168.127.137 --testInfo: Creating a new SSL key for ...

  5. CyNix-lxd提权

    0x01 信息收集 nmap -p- -T5 192.168.43.155扫描开放端口 nmap -sV -p 80,6688 -A 192.168.43.155 -oA cynix扫描指定端口 go ...

  6. SAP client锁定

    今天发现一个函数可以锁定SAP CLIENT . SCCR_LOCK_CLIENT 参数是client号码. 还可以通过事物SU10批量锁定用户登陆client

  7. Android 8.0/9.0 wifi 自动连接评分机制

    前言 Android N wifi auto connect流程分析 Android N selectQualifiedNetwork分析 Wifi自动连接时的评分机制 今天了解了一下Wifi自动连接 ...

  8. CentOS7,非LVM根分区扩容步骤:

    1.查看现有的分区大小 非LVM分区,目前磁盘大小为40G,根分区总容量为40G,(是自定义分区安装的) 2.关机增加磁盘大小至100G 如果你们是vmwaer虚拟软件安装的那如下入扩容: 3.查看磁 ...

  9. MySQL调优之查询优化

    一.查询慢的原因 1.网络 (1)网络丢包,重传 这个比较容易理解.当SQL 从客户端发送到数据库,执行完毕,数据库将结果返回给客户端,这个将数据返回给客户端的过程本质是网络包传输.因为链路的不稳定性 ...

  10. 1.4.1 对象与JSON转化 1.4.2 JSON与List集合转化 1.1.1 获取json中的属性 day10-05

    1.1.1 对象与JSON转化 @Test public void toJSON() throws IOException{ Jedis jedis = new Jedis("192.168 ...