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. mysql数据库导入和导出

    Mysql数据中,使用时,总是会碰见导入和导出情况,所以如何正确的导入导出,非常重要!下面根据工作中用到的方法,会不管补充: 导入: 直接在Mysql中导入: mysql>use databas ...

  2. EF OnModelCreating

      http://www.cnblogs.com/libingql/p/3353112.html     protected override void OnModelCreating(DbModel ...

  3. Mac OS X Terminal 101:终端使用初级教程

    Mac OS X Terminal 101:终端使用初级教程 发表于 2012 年 7 月 29 日 由 Renfei Song | 文章目录 1 为什么要使用命令行/如何开启命令行? 2 初识Com ...

  4. ViewPager中使用PhotoView时出现pointerIndex out of range异常

    问题描述: 当PhotoView 和 ViewPager 组合时 ,用双指进行放大时 是没有问题的,但是用双指进行缩小的时候,程序就会崩掉,并且抛出java.lang.IllegalArgumentE ...

  5. Nginx upstream 长连接

    原文: http://bollaxu.iteye.com/blog/900424 Nginx upstream目前只有短连接,通过HTTP/1.0向后端发起连接,并把请求的"Connecti ...

  6. OpenGL cubeMap

    glsl 的reflect(I,N)其中I是 眼睛(camera)位置到顶点位置的方向向量,N为顶点法线,必须要归一化 橙宝书里给出的计算过程是这样的:reflect(I,N) = I - 2 *do ...

  7. bash + script

    shell "" 保留$,`,\, 换行含义,‘’保留字面值 $(), ``用于命令替换 算术扩展如 $[1+1] for循环: for Host in host1, host2, ...

  8. Http 1.1协议

    HTTP是hypertext transfer protocol(超文本传输协议)的简写,它是TCP/IP协议的一个应用层协议,用于定义WEB浏览器与WEB服务器之间数据交换的过程. 1.Http1. ...

  9. C#参考:Linq 概述

    Linq (Language Integrated Query,语言集成查询),是微软公司提供的一项新技术,它能够将查询功能引入到.NET 3.5 所支持的编程语言中,例如C#,Visual Basi ...

  10. Android SDK开发包国内下载地址

    不知道是因为最近kaihui还是怎么的,打开android sdk官方网站特别的慢,想下载最新版本的platform几乎变成不可能完成的任务,不知道为什么Google不像Apache那样在各国设立镜像 ...