首页 技术 正文
技术 2022年11月6日
0 收藏 341 点赞 385 浏览 1238 个字

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 longusing 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;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289