Long Long Message

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <string>
#define LL long long
#define ULL unsigned long long using namespace std;
const int N = 1e5+10; ULL hash1[N],hash2[N],p[N];
int seed = 131;
char sa[N],sb[N]; void init()
{
p[0] = 1;
for(int i = 1; i <= 100000; i++)
{
p[i] = p[i-1]*seed;
}
} void hashs(char s[],ULL hashn[])
{
int len = strlen(s+1);
hashn[0] = 0;
hashn[1] = s[1]-'A'+1;
for(int i = 2; i <= len; i++)
hashn[i] = hashn[i-1]*seed + (s[i]-'A'+1);
} ULL getHash(int pos,int len, ULL hashn[]) //获取pos位置起长度为len的子字符串哈希值
{
//printf("hash: %ul\n",hashn[pos+len-1] - hashn[pos-1]*p[len]);
return hashn[pos+len-1] - hashn[pos-1]*p[len];
} bool check(int len, int la,int lb)
{
vector<ULL> bin;
for(int i = len; i <= la; i++)
bin.push_back(getHash(i-len+1,len,hash1)); sort(bin.begin(),bin.end()); for(int i = len; i <= lb; i++)
{
ULL temp = getHash(i-len+1,len,hash2);
if(binary_search(bin.begin(),bin.end(),temp))
return true;
}
return false;
} void solve()
{
init();
while(~scanf("%s %s",sa+1,sb+1))
{
hashs(sa,hash1);
hashs(sb,hash2);
int la = strlen(sa+1);
int lb = strlen(sb+1); int ans = 0;
int lf = 1,mid;
int rt = min(la,lb);
while(lf <= rt)
{
mid = (lf+rt)/2;
if(check(mid,la,lb))
{
ans = mid;
lf = mid+1;
}
else
{
rt = mid-1;
}
}
printf("%d\n",ans);
}
} int main(void)
{
solve(); return 0;
}

poj 2774 字符串哈希求最长公共子串的更多相关文章

  1. 【POJ 2774】Long Long Message 最长公共子串

    还是模板啊,手残&&打成||查错查了1h+TAT #include<cstdio> #include<cstring> #include<algorith ...

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

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

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

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

  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. 字符串hash + 二分答案 - 求最长公共子串 --- poj 2774

    Long Long Message Problem's Link:http://poj.org/problem?id=2774 Mean: 求两个字符串的最长公共子串的长度. analyse: 前面在 ...

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

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

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

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

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

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

随机推荐

  1. 《转》python对象

    http://www.cnblogs.com/BeginMan/p/3160044.html 一.学习目录 1.pyhton对象 2.python类型 3.类型操作符与内建函数 4.类型工厂函数 5. ...

  2. 如何通过SVN管理好代码

    来自:http://blog.csdn.net/baronyang/article/details/6942434 ------------------------------------------ ...

  3. android Toast提示异常:java.lang.RuntimeException: Can't create handler inside thread that has not called

    Toast只能在UI线程弹出,解决此问题可以在Toast前后加两行代码,如下所示: Looper.prepare(); Toast.makeText(getApplicationContext(),& ...

  4. leetcode-227-基本计算器②

    题目描述: 方法一:中缀转后缀 #!_*_coding:utf-8_*_ class Solution: def calculate(self, s: str) -> int: def in_t ...

  5. [JZOJ2865]【集训队互测 2012】Attack

    题目 题目大意 平面上有一堆带权值的点.两种操作:交换两个点的权值,查找一个矩形的第\(k\)小 \(N<=60000\) \(M<=10000\) \(10000ms\) 思考历程&am ...

  6. 查看linux的登录日志

    查看linux的登录日志 1. lastlog 列出所有用户最近登录的信息  lastlog引用的是/var/log/lastlog文件中的信息,包括login-name.port.last logi ...

  7. Android Matrix理论与应用详解

    转:http://zensheno.blog.51cto.com/2712776/513652 Matrix学习——基础知识 以前在线性代数中学习了矩阵,对矩阵的基本运算有一些了解,前段时间在使用GD ...

  8. thinkphp一键清除缓存的方法

    后台控制器: <?php namespace Home\Controller; use Think\Controller; class HuancuController extends Cont ...

  9. Linux 后台运行python .sh等程序,以及查看和关闭后台运行程序操作

    1.运行.sh文件 直接用./sh 文件就可以运行,但是如果想后台运行,即使关闭当前的终端也可以运行的话,需要nohup命令和&命令. (1)&命令 功能:加在一个命令的最后,可以把这 ...

  10. 安装MySQL出现的this application

    1,https://www.microsoft.com/zh-cn/download/confirmation.aspx?id=1385 2,安装Windows 图像组件 (WIC)以及NET Fra ...