首页 > 其他分享 >P3667 Bovine Genomics Hash+二分题解

P3667 Bovine Genomics Hash+二分题解

时间:2024-04-24 11:24:03浏览次数:16  
标签:Genomics ch Hash int 题解 mid len long check

砂金听说了你在学字符串,于是在CLOI里出了道题给你


P3667 Bovine Genomics

题链:洛谷 hzoi提高

\(hash\)基础题。

思路是二分答案,\(check\)中比较每一个区间字串的\(hash\)值是否相等。

比较的时候可以用\(set\)或\(map\)。

\(set\)的好处在于无重元素,判断时先将\(a\)串中区间子串的哈希值放入数组,再用\(find\)函数,每次寻找\(b\)串的区间子串的哈希值是否出现过。我个人用的\(set\),代码在下面。

\(map\)的话写起来差不多,值类型设为\(bool\),每次直接判断\(true\,or\,false\)即可。

代码如下:

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;
}

或一无所有

image


完结撒花

标签:Genomics,ch,Hash,int,题解,mid,len,long,check
From: https://www.cnblogs.com/Ratio-Yinyue1007/p/18154654

相关文章

  • P3667 [USACO17OPEN] Bovine Genomics G (set容器)
    [USACO17OPEN]BovineGenomicsG题目描述FarmerJohnowns\(N\)cowswithspotsand\(N\)cowswithoutspots.Havingjustcompletedacourseinbovinegenetics,heisconvincedthatthespotsonhiscowsarecausedbymutationsinthebovinegenome.Atgr......
  • 抢先看!美团、京东、360等大厂面试题解析,技术面试必备。
    技术面试必备!美团、京东、360等大厂面试题详解,让你轻松应对各大公司面试挑战!往期硬核面经哦耶!冲进腾讯了!牛逼!上岸腾讯互娱和腾讯TEG!腾讯的面试,强度拉满!前几篇文章分享了上岸腾讯的最新面经。不少粉丝股东留言说别只发腾讯的啦,其他大厂的也安排一些吧,比如美团、360、京东的......
  • [ABC329C] Count xxx 题解
    [ABC329C]Countxxx题解题目分析目的:统计本质不同而不是位置不同的所有字符都相同的字串。需要理解一下什么是本质不同而不是位置不同。结合样例1去理解这句话。列举样例1中的所有所有组成字符相同的字串。aaabaa编号字串位置\(1\)a\([1,1]\)\(2\)aa\([1......
  • [题解]ARC176 A~B
    赛时心态崩了,0pts遗憾离场……今天在学校冷静思考了下。发现B题思路其实很简单,不过A题怎么也没有想到,回来看了题解,其实思路也很简单,不过是自己思考方向错了。看来打比赛心态很重要,如果能冷静下来思考结果会好很多。果然算法竞赛不能被常理所束缚(笑)A-01MatrixAgain行列从\(0......
  • 题解 UOJ577【[ULR #1] 打击复读】
    题解UOJ577【[ULR#1]打击复读referencehttps://www.cnblogs.com/crashed/p/17382894.htmlhttps://www.cnblogs.com/sizeof127/articles/17579027.html字符串——黄建恒,广东实验中学题目描述为了提升搜索引擎的关键词匹配度以加大访问量,某些网站可能在网页中无意义复读大......
  • 编译用于Qt的opencv问题解决
    CMakewasunabletofindabuildprogramcorrespondingto"MinGWMakefiles"解释:这个错误表明CMake无法找到用于生成Makefiles的构建程序。在使用CMake生成项目文件时,如果指定了"MinGWMakefiles",CMake需要一个Make工具来构建项目,而这个工具通常是由MinGW提供的。如......
  • macOS配置Clion用于STM32开发找不到stdint.h等头文件问题解决方案
    问题编译工程时发现出现大量类似错误如下/opt/homebrew/Cellar/arm-none-eabi-gcc/13.2.0/lib/gcc/arm-none-eabi/13.2.0/include/stdint.h:9:16:fatalerror:stdint.h:Nosuchfileordirectory问题原因不能使用brewinstallarm-none-eabi-gcc安装编译工具链[1]解决方......
  • 「洛谷」题解:P1720 月落乌啼算钱(斐波那契数列)
    题目传送门比较经典的一道斐波那契数列的模版题,原题中给了一个很复杂的公式(也就是下面这个),但是实际上题目跟它毛关系没有……(所以放这个公式干什么)\[F_n=\dfrac{\left(\frac{1+\sqrt{5}}{2}\right)^n-\left(\frac{1-\sqrt{5}}{2}\right)^n}{\sqrt{5}}\]看见题解去了有很多人都......
  • AtCoder Beginner Contest 350 A - G 题解
    AtCoderBeginnerContest350A-PastABCsSolution把最后三个字符转成数字判断即可Code#include<bits/stdc++.h>usingnamespacestd;intmain(){strings;cin>>s;s=s.substr(3,3);intx=0;x=(s[0]-'0')*100+(s[1]-�......
  • 模块(time、datetime、os、random、日志logging、hashlib)
    【一】time模块【1】表示时间的三种方式时间戳元组(struct_time)格式化的时间字符串:格式化的时间字符串(FromatString):'1999-12-06'【2】时间转换(1)导入时间模块importtime(2)时间戳[1]生成时间戳importtime#生成时间戳,时间戳是浮点数类型time_str=time.time......