字符串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 ...
随机推荐
- JSON编码格式提交表单数据详解
以JSON编码格式提交表单数据是HTML5对WEB发展进化的又一大贡献,以前我们的HTML表单数据是通过key-value方式传输的服务器端,这种形式的传输对数据组织缺乏管理,形式十分原始.而新出现的 ...
- BZOJ 3732: Network 最小生成树 倍增
3732: Network 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=3732 Description 给你N个点的无向图 (1 &l ...
- 在DataTable中执行DataTable.Select("条件")返回DataTable;
转:http://blog.csdn.net/hcf_force/article/details/7779062 1.在DataTable中执行DataTable.Select("条件&qu ...
- ECshop导入淘宝数据包乱码问题解决方法
ECshop在导入淘宝数据包的时候出现数据乱码. 测试版本 ecshop2.73 利用淘宝助手导出一个数据包(.csv),不要一次全部商品导出,最好是将数据包控制在1M左右,因为ecshop对上传文件 ...
- Tomcat抛出异常:ClientAbortException: java.net.SocketException: Connection
在做一个小网站的时候,写了一个通过servlet实现文件下载功能的页面.当我点击超级练级,弹出下载对话框,点击保存正常下载,不会出现任何问题,当我我点击取消,服务器端就出现如下提示: ClientAb ...
- 单击HighCharts柱形体弹框显示详细信息
上篇博客介绍了如何在HighCharts统计图表下生成表格,二者相互结合,可以对数据进行更好的统计分析.在统计的同时,如果需要想要及时查看详细信息的话,就得再加一个功能,就是单击柱形体,显示该项下的详 ...
- SAP GUI SAPLOGON.INI
GUI是SAP系统最常用的客户端,在一台客户机上,利用GUI可以连接多套SAP系统(连接方法参见<客户端连接配置(SAP GUI 710)>),也可以设置多个快捷方式登录(参见<用快 ...
- bootstrap fileinput 文件上传工具
这是我上传的第二个plugin 首先第一点就是因为这个好看 符合bootstrap的界面风格 第二是可以拖拽(虽然我不常用这个功能 但是这样界面看起来就丰满了很多) 最后不得不吐槽这个的回发事件 我百 ...
- 严重: Exception starting filter struts2解决方法!
转自:http://blog.knowsky.com/260578.htm 问题出现: 严重: Exception starting filter struts2java.lang.ClassNotF ...
- 在linux下安装Xwindows
检查Linux系统是否能够联网. 执行命令 yum -y groupinstall Desktop 等上面的命令执行完后,再执行这条命令 yum -y groupinstall "X Win ...