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. JMeter安装和环境变量搭建

    下载安装Java JDK环境,设置环境变量 elasticsearch for windows:https://www.cnblogs.com/Neeo/articles/10368280.html ...

  2. Neo4j中實現自定義中文全文索引

    資料庫檢索效率時,一般首要優化途徑是從索引入手,然後根據需求再考慮更復雜的負載均衡.讀寫分離和分散式水平/垂直分庫/表等手段:索引通過資訊冗餘來提高檢索效率,其以空間換時間並會降低資料寫入的效率,因此 ...

  3. Git查看历史记录的几种方法

  4. FCC知识点总结

    1.DOMContentLoaded事件 2.延迟脚本 defer 3.异步脚本async 4.[找最长单词]—— 找出句子中最长的单词,并返回它的长度. 5.数组slice().splice() s ...

  5. 尝试 zabbix 小记

    server : Ubuntu 16.04 zabbix: 2.2.23源码包 安装 gcc,curl,make,snmp 软件和zabbix依赖一些php 扩展包 sudo apt-get inst ...

  6. Neo4j 因果集群搭建及neo4j-java-driver连接

    搭建Neo4j因果集群 1.下载企业版,当前是3,5,9版本 https://neo4j.com/download-center/#enterprise 2.配置,三个核心集群为例 配置文件,conf ...

  7. Flink SQL 系列 | 5 个 TableEnvironment 我该用哪个?

    本文为 Flink SQL 系列文章的第二篇,前面对 Flink 1.9 Table 新架构及 Planner 的使用进行了详细说明,本文详细讲解 5 个 TableEnvironment 及其适用场 ...

  8. thinkphp url生成

    为了配合所使用的URL模式,我们需要能够动态的根据当前的URL设置生成对应的URL地址,为此,ThinkPHP内置提供了U方法,用于URL的动态生成,可以确保项目在移植过程中不受环境的影响. 定义规则 ...

  9. springboot2.x jpa接入多数据源

    环境:springboot 2.1.4 数据源引入方式 数据源一 @Configuration @EnableTransactionManagement @EnableJpaRepositories( ...

  10. 5020: [THUWC 2017]在美妙的数学王国中畅游

    传送门 当年听llj讲的时候觉得这简直是个不可做的神题. 现在看来并不是很神,可能是我已经被剧透了的缘故... 一开始以为是函数套函数,懵蔽了好久,结果只是求和 被剧透了泰勒展开就比较水了..只要你不 ...