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的更多相关文章

  1. 后缀数组(模板题) - 求最长公共子串 - poj 2774 Long Long Message

    Language: Default Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 21 ...

  2. POJ 2774 Long Long Message (二分 + Hash 求最长公共子串)题解

    题意:求最长公共子串 思路:把两个串Hash,然后我们把短的作为LCS的最大可能值,然后二分长度,每次判断这样二分可不可以.判断时,先拿出第一个母串所有len长的子串,排序,然后枚举第二个母串len长 ...

  3. poj 2774 Long Long Message,后缀数组,求最长公共子串 hdu1403

    题意:给出两个字符串,求最长公共子串的长度. 题解:首先将两个字符串连在一起,并在中间加一个特殊字符(字串中不存在的)切割,然后两个串的最长公共字串就变成了全部后缀的最长公共前缀.这时就要用到heig ...

  4. 求最长公共子串 Longest Common Subsequence

    最长公共子串 // Longest Common Subsequence 子串有别于子序列, 子串是连续的, 而子序列可以不连续 /*--------------------------------- ...

  5. 文本比较算法Ⅱ——Needleman/Wunsch算法的C++实现【求最长公共子串(不需要连续)】

    算法见:http://www.cnblogs.com/grenet/archive/2010/06/03/1750454.html 求最长公共子串(不需要连续) #include <stdio. ...

  6. poj2774 Long Long Message 后缀数组求最长公共子串

    题目链接:http://poj.org/problem?id=2774 这是一道很好的后缀数组的入门题目 题意:给你两个字符串,然后求这两个的字符串的最长连续的公共子串 一般用后缀数组解决的两个字符串 ...

  7. java求最长公共子串的长度

    1这道题目就是给定两个字符串,然后求这两个字符串的最长公共子串的最大长度,假设我的f()方法是来求两个字符串的最大公共子串,从头开始逐一比较,如果相等,则 继续调用这个方法,使得递归的长度+1,如果不 ...

  8. 利用后缀数组(suffix array)求最长公共子串(longest common substring)

    摘要:本文讨论了最长公共子串的的相关算法的时间复杂度,然后在后缀数组的基础上提出了一个时间复杂度为o(n^2*logn),空间复杂度为o(n)的算法.该算法虽然不及动态规划和后缀树算法的复杂度低,但其 ...

  9. poj 2774 字符串哈希求最长公共子串

    Long Long Message #include <iostream> #include <algorithm> #include <cstdio> #incl ...

随机推荐

  1. Mac OS 中设置VPN(pptp连接方式)

    第一步:点击系统设置(如图) 第二步:点击网络,进入网络设置(如图) 第三步:点击+号-创建新服务,创建新的VPN网络连接(如图) 第四步:接口-中选-VPN, VPN类型-中选-PPTP,服务名称- ...

  2. 微信中直接下载APK

    某天在微信中偶遇一个二维码,识别二维码竟然可以直接下载APK! 该二维码如下: 解码后获得地址:(在线解码工具) http://www.rmdown.com/newt66y.apk 这不就是个普通的A ...

  3. 一台服务器部署多个tomcat

    bin 目录下两文件 catalina.sh.startup.sh conf  目录下一个文件 server.xml

  4. Myeclipse 2015 stable 1.0 完美破解方法(转自 http://yangl.net/2015/07/14/myeclipse_2015stable_1/)

    Myeclipse 2015 stable 1.0 完美破解方法 http://yangl.net/2015/07/14/myeclipse_2015stable_1/ 破解包(注册机)下载地址:链接 ...

  5. Clappr——开源的Web视频播放器

    巴西著名的门户网站Globo.com(视频播放器),使用的是基于OSMF的Flash组件.在最近几年的发展过程中,Globo为视频平台陆续添加了不少额外功能,例如: 字幕,广告,画中画播放等.然而,由 ...

  6. 使用UIKit制作卡牌游戏(三)ios游戏篇

    译者: Lao Jiang | 原文作者: Matthijs Hollemans写于2012/07/13 转自朋友Tommy 的翻译,自己只翻译了这第三篇教程. 原文地址: http://www.ra ...

  7. SQL Server 2008 Windows身份验证改为混合模式身份验证

    1.在当前服务器右键进入“属性页”->“安全性”->勾选Sql Server和Windows身份验证模式->确定. 由于默认不启用sa,所以如果启用sa账户登录,则还需要如下设置: ...

  8. [转]C# dataGridview 报“索引-1没有值”的解决办法

    很多WINFORM的开发人员在DataGridView的开发当中,都会出现“索引-1没有值”这个烦人的问题,其实较早之前,我已经大概知道问题的所在,也找到了解决方法,不过一直没有时间去深入研究一下,今 ...

  9. cocos2d-x开发: 场景实体(entity)管理

    公司现在开新项目,主题的框架部分都是我自己在做,不用受到别人的牵制,所以还算是比较的自由,很好发挥. 游戏并不大,所以需要用到的地方并不多.今天花了一些时间写了场景entity管理的部分代码,还没有完 ...

  10. MyBatis知多少(14)分散的数据库系统

    任何一个重要的数据库无疑都会拥有不止一个依赖者.即使该数据库只是简单地被两个Web 应用程序所共享,也有许多事情需要考虑.假设有一个名为网上购物车的Web应用程序,它使用了一个包含类别代码的数据库.就 ...