CF1320D Reachable Strings

题目

分析

像这样多次询问的东西,一般都是一段含有某些一定的特征,我们才能加以判断,所以我们的目的就是寻找这些变化中不变的特征。

需要观察性质,发现在这样的操作当中,每一个 \(0\) 之间的相对位置不会变,并且每一个 \(0\) 的奇偶性一定不变。

于是我们可以想到直接哈希来判断,只要两个区间,每一个相对位置的 \(0\) 的奇偶性是相同的,那么就视为这两个区间相同。

具体实现见代码。

代码

#include<bits/stdc++.h>
using namespace std;
//#ifdef ONLINE_JUDGE
//	#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
//	char buf[1<<21],*p1=buf,*p2=buf;
//#endif
template<typename T>
inline void read(T &x){
	x=0;bool f=false;char ch=getchar();
	while(!isdigit(ch)){f|=ch=='-';ch=getchar();}
	while(isdigit(ch)){x=x*10+(ch^48);ch=getchar();}
	x=f?-x:x;
	return ;
}
template<typename T>
inline void write(T x){
	if(x<0) x=-x,putchar('-');
	if(x>9) write(x/10);
	putchar(x%10^48);
	return ;
}
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pc putchar
#define PII pair<int,int>
#define rep(i,x,y) for(register int i=(x);i<=(y);i++)
#define dep(i,y,x) for(register int i=(y);i>=(x);i--)
#define repg(i,x) for(int i=head[x];i;i=nex[i])
#define filp(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
#define infilp(s) freopen(s".in","r",stdin)
#define outfilp(s) freopen(s".out","w",stdout)
const int MOD=998244353;
inline int inc(int x,int y){x+=y;return x>=MOD?x-MOD:x;}
inline int dec(int x,int y){x-=y;return x<0?x+MOD:x;}
inline void incc(int &x,int y){x+=y;if(x>=MOD) x-=MOD;}
inline void decc(int &x,int y){x-=y;if(x<0) x+=MOD;}
inline void chkmin(int &x,int y){if(y<x) x=y;}
inline void chkmax(int &x,int y){if(y>x) x=y;}
const int N=2e5+5,M=2e5+5,INF=1e9+7;
const int base=19260817;
int n,q,num[N];
char str[N];
ll ha[N][2],Pow[N];
inline bool CheckHash(int l1,int l2,int len,int op1,int op2){
	int r1=l1+len-1,r2=l2+len-1;
//	cout<<dec(ha[r1][op1],ha[l1-1][op1]*Pow[num[r1]-num[l1-1]]%MOD)<<' '<<dec(ha[r2][op2],ha[l2-1][op2]*Pow[num[r2]-num[l2-1]]%MOD)<<' '<<ha[r2][op2]<<' '<<ha[l2-1][op2]<<' '<<Pow[num[r2]-num[l2-1]]<<endl;
	return 
	dec(ha[r1][op1],ha[l1-1][op1]*Pow[num[r1]-num[l1-1]]%MOD)==dec(ha[r2][op2],ha[l2-1][op2]*Pow[num[r2]-num[l2-1]]%MOD);
}
signed main(){
//	double ST=clock();
	// ios::sync_with_stdio(false);
//#ifndef ONLINE_JUDGE
//	filp("my");
//#endif
	read(n);
	scanf("%s",str+1);
	Pow[0]=1;
	for(int i=1;i<=n;i++){
		if(str[i]=='1') ha[i][0]=ha[i-1][0],ha[i][1]=ha[i-1][1],num[i]=num[i-1];
		else{
			if(i&1) ha[i][1]=(ha[i-1][1]*base+1)%MOD,ha[i][0]=(ha[i-1][0]*base+2)%MOD;
			else ha[i][0]=(ha[i-1][0]*base+1)%MOD,ha[i][1]=(ha[i-1][1]*base+2)%MOD;
			num[i]=num[i-1]+1;
		}
		Pow[i]=Pow[i-1]*base%MOD;
	}
	read(q);
	while(q--){
		int p1,p2,len;
		read(p1),read(p2),read(len);
		if(CheckHash(p1,p2,len,p1&1,p2&1)) puts("Yes");
		else puts("No");
	}
//	cerr<<"\nTime:"<<(clock()-ST)/CLOCKS_PER_SEC<<"s\n";
	return 0;
}
/*
100
0011011100000010101111011010010000010101000011111110101001010110110011000100001110011001111110100110
2
17 99 2
2 5 2

4
1010
2
1 3 2
*/
原文地址:https://www.cnblogs.com/Akmaey/p/15617899.html