字符串hash + 二分答案 - 求最长公共子串 --- poj 2774
Long Long Message
Problem's Link:http://poj.org/problem?id=2774
Mean:
求两个字符串的最长公共子串的长度。
analyse:
前面在学习后缀数组的时候已经做过一遍了,但是现在主攻字符串hash,再用字符串hash写一遍。
这题的思路是这样的:
1)取较短的串的长度作为high,然后二分答案(每次判断长度为mid=(low+high)>>1是否存在,如果存在就增加下界;不存在就缩小上界);
2)主要是对答案的判断(judge函数)。具体参看代码注释。
Time complexity:O(n)
Source code:
// Memory Time
// 1347base 0MS
// by : Snarl_jsb
// 2014-10-04-21.16
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<string>
#include<climits>
#include<cmath>
#define ULL unsigned long long
using namespace std; string s1,s2;
int l1,l2,seed=131;
vector<ULL> hash;
bool judge(int x)
{
hash.clear();
ULL tmp=0;
for (int i = 0; i < x; i++)
{
tmp=tmp* seed + s1[i];
}
hash.push_back(tmp);
ULL base =1;
for (int i = 1; i < x; i++)
{
base *= seed;
}
for (int i = x; i < l1; i++)
{
tmp=(tmp*seed+s1[i])-base*s1[i-x]*seed;
hash.push_back(tmp);
}
sort(hash.begin(),hash.end());
ULL hashval = 0;
for (int i = 0; i < x; i++)
{
hashval = hashval * seed + s2[i];
}
if (binary_search(hash.begin(),hash.end(),hashval))
return 1;
for (int i = x; i < l2; i++)
{
hashval = (hashval-(s2[i-x])*base)*seed+s2[i];
if (binary_search(hash.begin(),hash.end(),hashval))
return 1;
}
return 0;
}
int main()
{
while (cin>>s1>>s2)
{
l1=s1.size();
l2=s2.size();
int ans = 0;
int high = min(l1,l2);
int low = 0; while (low <= high)
{
int mid = (low+high)>>1;
if (judge(mid))
{
ans = mid;
low = mid+1;
}
else
high = mid-1;
}
printf("%d\n",ans);
}
return 0;
}
注释代码:
// Memory Time
// 1347k 0MS
// by : Snarl_jsb
// 2014-10-04-21.16
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<string>
#include<climits>
#include<cmath>
#define ULL unsigned long long
using namespace std; string s1,s2;
int l1,l2,seed=131;
vector<ULL> hash;
bool judge(int x)
{
hash.clear();//多组数据时不要忘了清空全局数组
//构造s1串的hash表
ULL tmp=0;
for (int i = 0; i < x; i++)
{
tmp=tmp* seed + s1[i];
}
hash.push_back(tmp);
ULL base =1;
for (int i = 1; i < x; i++)//求出到达x的base值
{
base *= seed;
}
for (int i = x; i < l1; i++)
{
tmp=(tmp*seed+s1[i])-base*s1[i-x]*seed;
hash.push_back(tmp);
}
//构造完毕
sort(hash.begin(),hash.end()); //二分查找加速,必需先排序
ULL hashval = 0;
for (int i = 0; i < x; i++)//求出s2串0到x的hash值
{
hashval = hashval * seed + s2[i];
}
if (binary_search(hash.begin(),hash.end(),hashval))//查找s2串0到x的hash值是否在s1串的hash表中
return 1;
for (int i = x; i < l2; i++)//如果上面的s2串0到x的hash值未匹配成功,这儿接着匹配s2串长度为x的hash值是否出现在s1串的hash表中
{
hashval = hashval*seed+s2[i]-s2[i-x]*base*seed;
if (binary_search(hash.begin(),hash.end(),hashval))
return 1;
}
return 0;
}
int main()
{
while (cin>>s1>>s2)
{
l1=s1.size();
l2=s2.size();
int ans = 0;
int low=0,high = min(l1,l2);
while (low <= high)//二分答案
{
int mid = (low+high)>>1;
if (judge(mid))//判断答案是否可行
{
ans = mid;
low = mid+1;
}
else
high = mid-1;
}
printf("%d\n",ans);
}
return 0;
}
字符串hash + 二分答案 - 求最长公共子串 --- poj 2774的更多相关文章
- 后缀数组(模板题) - 求最长公共子串 - poj 2774 Long Long Message
Language: Default Long Long Message Time Limit: 4000MS Memory Limit: 131072K Total Submissions: 21 ...
- POJ 2774 Long Long Message (二分 + Hash 求最长公共子串)题解
题意:求最长公共子串 思路:把两个串Hash,然后我们把短的作为LCS的最大可能值,然后二分长度,每次判断这样二分可不可以.判断时,先拿出第一个母串所有len长的子串,排序,然后枚举第二个母串len长 ...
- poj 2774 Long Long Message,后缀数组,求最长公共子串 hdu1403
题意:给出两个字符串,求最长公共子串的长度. 题解:首先将两个字符串连在一起,并在中间加一个特殊字符(字串中不存在的)切割,然后两个串的最长公共字串就变成了全部后缀的最长公共前缀.这时就要用到heig ...
- 求最长公共子串 Longest Common Subsequence
最长公共子串 // Longest Common Subsequence 子串有别于子序列, 子串是连续的, 而子序列可以不连续 /*--------------------------------- ...
- 文本比较算法Ⅱ——Needleman/Wunsch算法的C++实现【求最长公共子串(不需要连续)】
算法见:http://www.cnblogs.com/grenet/archive/2010/06/03/1750454.html 求最长公共子串(不需要连续) #include <stdio. ...
- poj2774 Long Long Message 后缀数组求最长公共子串
题目链接:http://poj.org/problem?id=2774 这是一道很好的后缀数组的入门题目 题意:给你两个字符串,然后求这两个的字符串的最长连续的公共子串 一般用后缀数组解决的两个字符串 ...
- java求最长公共子串的长度
1这道题目就是给定两个字符串,然后求这两个字符串的最长公共子串的最大长度,假设我的f()方法是来求两个字符串的最大公共子串,从头开始逐一比较,如果相等,则 继续调用这个方法,使得递归的长度+1,如果不 ...
- 利用后缀数组(suffix array)求最长公共子串(longest common substring)
摘要:本文讨论了最长公共子串的的相关算法的时间复杂度,然后在后缀数组的基础上提出了一个时间复杂度为o(n^2*logn),空间复杂度为o(n)的算法.该算法虽然不及动态规划和后缀树算法的复杂度低,但其 ...
- poj 2774 字符串哈希求最长公共子串
Long Long Message #include <iostream> #include <algorithm> #include <cstdio> #incl ...
随机推荐
- 常用的sql脚本 游标遍历操作
Declare @id int DECLARE cursor_data CURSOR FOR --这里是取得数据源 OPEN cursor_data FETCH FROM cursor_data IN ...
- Windows下安装 msysGit 以及初始化 Git server环境
Windows下git工具msysGit使用以及Git server初始化 Windows下git工具,这里选择msysGit,版本为msysGit-netinstall-1.8.1.2-previe ...
- 理解 python metaclass使用技巧与应用场景分析
理解python metaclass使用技巧与应用场景分析 参考: decorator与metaclass:http://jfine-python-classes.readthedocs. ...
- linux 系统性能指标采样脚本
以下脚本写于redmine性能排查时,用于定位系统性能瓶颈的采样,源地址为~/performanceLog/collectLog.sh中,计划放入github的代码片段库中. 注: 如果mysql的地 ...
- php 方便快捷导出excel
/** * 导出excel * @param array $column * eg: $column = [ * 'username' => '姓名', * 'age' => '年龄', ...
- 在Windows 2008/2008 R2 上配置IIS 7.0/7.5 故障转移集群
本文主要是从:http://support.microsoft.com/kb/970759/zh-cn,直接转载,稍作修改裁剪而来,其中红色粗体部分,是我特别要说明的 若要配置 IIS 7.0 和 7 ...
- Android Studio开发入门-引用jar及so文件
作者:王先荣 最近初学安卓开发,因为以前从未用过JAVA,连基本的语法都要从头开始,所以不太顺利.在尝试使用百度语音识别引擎时遇到了如何引用jar及so文件的问题.在GOOGLE加多次尝试之后, ...
- Mysql数据备份与恢复
REM INIT→既に指定したファイルが有った場合, 上書きする REM NORECOVERY→接続先データベースへの既存の接続を閉じる REM REPLACE→既存のデータベースを上書きする set ...
- Mac 下安装mitmproxy
环境: Mac OS X 10.9.4 1. 安装 直接用pip 安装 pip install mitmproxy 发现在安装依赖包 lxml 的时候报错 In : /private/tmp/pip ...
- centos7.0 手动编译 lamp环境
首先新建用户 lamper,并添加 sodu权限 两种方法:is not in the sudoers file 解决(转) xx is not in the sudoers file 问题解决[转载 ...