P3667 Bovine Genomics Hash+二分题解
砂金听说了你在学字符串,于是在CLOI里出了道题给你
P3667 Bovine Genomics
思路是二分答案,
比较的时候可以用
代码如下:
map<ull,bool>m;
bool check(int len)
{
int re=0;
for(int i=1;i+len-1<=m;i++)
{
bool kk=false;
m.clear();
int j=i+len-1;
fo(k,1,n)
m[Aget_hash(i,j,k,1)]=true;
fo(k,1,n)
if(m[Aget_hash(i,j,k,0)])
{
kk=true;
break;
}
if(!kk)
{
re=1;
break;
}
}
return re;
}
完整代码:
所有
#include<bits/stdc++.h>
#define fo(x,y,z) for(register int (x)=(y);(x)<=(z);(x)++)
#define fu(x,y,z) for(register int (x)=(y);(x)>=(z);(x)--)
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
namespace Dr
{
inline int qr()
{
char ch=getchar();int x=0,f=1;
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=(x<<3)+(x<<1)+(ch^48);
return x*f;
}
inline void qw(ull x)
{
if(!x)
return;
qw(x/10);
putchar(x%10+'0');
}
}
#define qr qr()
using namespace Dr;
const int Ratio=0;
const int N=1005;
const int maxi=INT_MAX;
int lena,lenb,n,m;
ull p[N],ha[N][N],hb[N][N];
char a[N][N],b[N][N];
ull base=233,ans;
namespace Acheronhash
{
void Aprepare()
{
p[0]=1;
fo(i,1,500)
p[i]=p[i-1]*base;
}
ull Aget_hash(int l,int r,int id,int x)
{
if(x)
return ha[id][r]-ha[id][l-1]*p[r-l+1];
else
return hb[id][r]-hb[id][l-1]*p[r-l+1];
}
// ull Aget_s(int l,int r,int x)
// {
// return Aget_hash(l,x-1)*p[r-x]+Aget_hash(x+1,r);
// }
ull Aget_hashall(char c[])
{
int len=strlen(c);
ull ans=0;
fo(i,0,len-1)
ans=ans*base+(ull)c[i];
return ans;
}
void Aget_hasheverya(int id)
{
fo(i,1,m)
ha[id][i]=ha[id][i-1]*base+a[id][i];
}
void Aget_hasheveryb(int id)
{
fo(i,1,m)
hb[id][i]=hb[id][i-1]*base+b[id][i];
}
}
using namespace Acheronhash;
set<ull>f;
bool check(int len)
{
int re=0;
for(int i=1;i+len-1<=m;i++)
{
bool kk=false;
f.clear();
int j=i+len-1;
fo(k,1,n)
f.insert(Aget_hash(i,j,k,1));
fo(k,1,n)
if(f.find(Aget_hash(i,j,k,0))!=f.end())
{
kk=true;
break;
}
if(!kk)
{
re=1;
break;
}
}
return re;
}
int main()
{
Aprepare();
n=qr,m=qr;
fo(i,1,n)
{
scanf("%s",a[i]+1);
Aget_hasheverya(i);
}
fo(i,1,n)
{
scanf("%s",b[i]+1);
Aget_hasheveryb(i);
}
int l=1,r=m;
while(l<=r)
{
int mid=(l+r)>>1;
if(check(mid))
ans=mid,r=mid-1;
else
l=mid+1;
}
printf("%lld\n",ans);
return Ratio;
}
或一无所有
完结撒花
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】