poj 2772 Long Long Message

求解最长公共字串使用二分+字符串哈希

二分得到长度M,然后分别哈希两个字符串长度为M的子串,看两个字符串的哈希值是否有相等的,然后不断二分直到长度最大。

#include <algorithm>
#include <iostream>  
#include <cstring>    
#include <cstdio>    
#include <vector>  
#include <queue>  
#include <map>  
  
using namespace std;    

const long long B = 1e8+7;
const int MAX = 100010;
int n,m;
char s1[MAX],s2[MAX];
long long base[MAX],hash[MAX];

bool check(int len){
	long long tmp = 0;
	for(int i=0; i<len; i++)
		tmp = tmp * B + s1[i];
	hash[0] = tmp;
	for(int i=0; i<=n-len; i++)
		hash[i+1] = hash[i]*B + s1[i+len] - s1[i]*base[len];//通过一个哈希值求所有长度为len的字串的哈希值(B进制的数字)
	sort(hash, hash+n-len+1);//二分查找先排序
	long long Hash = 0;
	for(int i=0; i<len; i++) Hash = Hash*B + s2[i];
	for(int i=0; i<m-len+1; i++){
		if(binary_search(hash, hash+n-len+1, Hash)) return true;
		Hash = Hash*B + s2[i+len]-s2[i]*base[len];
	}
	return false;
}

int main(){
	base[0] = 1;
	for(int i=1; i<MAX; i++) base[i] = base[i-1]*B;
	scanf("%s",s1);
	scanf("%s",s2);
	n = strlen(s1);
	m = strlen(s2);
	int ans = -1;
	int l = 0, r = min(n, m);
	while(l <= r){
		int mid = (l + r) >> 1;
		if(check(mid)){//mid可行那么向右查找是否有更大的答案
			l = mid + 1;
			ans = mid;
		}
		else r = mid - 1;
	}
	printf("%d\n",ans);
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值