CF1320D Reachable Strings
110和011互相转化,相当于就是0在连续两个1的情况下,移动两个位置
能够发现,0的位置的奇偶不会改变,且很多个0之间的相对位置不会改变
猜想考虑这个答案只跟0的奇偶性有关,下面小证一下:(注意下面所说的“奇偶”指的是两个字符串的分别第一个字母为奇数时的奇偶,不是总字符串的奇偶)
若0的相对位置奇偶不一样,显然非法
若0的相对位置奇偶一样,是否合法?假定我们把两个字符串的0都尽可能往左移动,再判断是否相等,这个方法正确性显然。
现在假设我已经将两个字符串尽可能把0往右移动了,如果是非法情况,也就是说有一个字符串无法移动到下一个和它奇偶性相同且另外一个字符串的存在0的那个位置,那么子串不可能出现连续两个1,必然会有0将他们隔断,然后就不符合上面假设的相对位置奇偶相同了
所以,直接对奇偶比较是正确的,直接奇偶起点分别哈希就行了,实现题解有很多,具体实现注意求一段子串哈希并不能用传统的方式求
#include<bits/stdc++.h>
#define il inline
#define maxn 200005
using namespace std;
typedef long long ll;
const int base=131;
const ll mod=998244353;
il int read(){
char c;int x=0,f=0;
while(!isdigit(c=getchar()))f|=(c=='-');
while(isdigit(c))x=(x*10)+(c^48),c=getchar();
return f?-x:x;
}
int n;
char s[maxn];
ll hs1[maxn],hs2[maxn],bs[maxn],id[maxn];
il int geths(int l,int r,int p){
if(p)return ((hs1[r]-hs1[l-1]*bs[id[r]-id[l-1]]%mod)%mod+mod)%mod;
else return ((hs2[r]-hs2[l-1]*bs[id[r]-id[l-1]]%mod)%mod+mod)%mod;
}
int main(){
n=read();
scanf("%s",(s+1));
bs[0]=1;
for(int i=1;i<=n;i++){
bs[i]=(bs[i-1]*base)%mod,id[i]=id[i-1];
if(s[i]=='1')hs1[i]=hs1[i-1],hs2[i]=hs2[i-1];
else{
hs1[i]=hs1[i-1]*base+1+(i&1),hs1[i]%=mod;
hs2[i]=hs2[i-1]*base+1+((i&1)^1),hs2[i]%=mod;
id[i]++;
}
}
int t=read();
while(t--){
int l1=read(),l2=read(),len=read();
int r1=l1+len-1,r2=l2+len-1;
geths(l1,r1,l1&1)==geths(l2,r2,l2&1)?puts("Yes"):puts("No");
}
return 0;
}
/*
3
010
1
1 3 1
*/
CF1320D Reachable Strings的更多相关文章
- 题解-Reachable Strings
题解-Reachable Strings 前置知识: \(\texttt{Hash}\) Reachable Strings 给一个长度为 \(n\) 的 \(\texttt{01}\) 串 \(s\ ...
- CF1320 Div1 D.Reachable Strings 题解
题目大意 给定一个长为\(n\)的01串\(S\),每次你可以对一个串的三个连续位置做:\(011 \rightarrow 110\),\(110 \rightarrow 011\)的操作. 有\(q ...
- Codeforces Round #625 (1A - 1D)
A - Journey Planning 题意: 有一列共 n 个城市, 每个城市有美丽值 b[i], 要访问一个子序列的城市, 这个子序列相邻项的原项数之差等于美丽值之差, 求最大的美丽值总和. ...
- 【Virt.Contest】CF1321(div.2)
第一次打虚拟赛. CF 传送门 T1:Contest for Robots 统计 \(r[i]=1\) 且 \(b[i]=0\) 的位数 \(t1\) 和 \(r[i]=0\) 且 \(b[i]=1\ ...
- Hacker Rank: Two Strings - thinking in C# 15+ ways
March 18, 2016 Problem statement: https://www.hackerrank.com/challenges/two-strings/submissions/code ...
- StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing the strings?
StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing t ...
- Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- [LeetCode] Add Strings 字符串相加
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...
- [LeetCode] Encode and Decode Strings 加码解码字符串
Design an algorithm to encode a list of strings to a string. The encoded string is then sent over th ...
- [LeetCode] Group Shifted Strings 群组偏移字符串
Given a string, we can "shift" each of its letter to its successive letter, for example: & ...
随机推荐
- Linux 环境下制作 deb 软件包
一.简介 前面的笔记中已经展示过了,怎么移植的一个工具境到 ARM 环境中,对于使用 buildroot 和 yocto 的朋友来说,此笔记就没有作用了,因为管理工具包会帮我们把这个工作处理了,就算需 ...
- Failed to download metadata for repo ‘PowerTools‘: Cannot prepare internal mirrorlist: No URLs in mi
Failed to download metadata for repo 'AppStream' [CentOS] - Techglimpsehttps://techglimpse.com/faile ...
- SpringBoot-Http请求工具类
一.编写请求配置类 import com.alibaba.fastjson.JSONObject; import org.springframework.context.annotation.Conf ...
- StarCoder2-Instruct: 完全透明和可自我对齐的代码生成
指令微调 是一种技术,它能让大语言模型 (LLMs) 更好地理解和遵循人类的指令.但是,在编程任务中,大多数模型的微调都是基于人类编写的指令 (这需要很高的成本) 或者是由大型专有 LLMs 生成的指 ...
- .NET 中的表达式树
.NET 中的表达式树(Expression Trees) 表达式树是什么? 表达式树(Expression Trees)是.NET框架中的一个强大功能,它将代码表示为一个由表达式节点组成的树形结构. ...
- vite.config.js 无法使用__dirname的解决方法
__dirname 是commonjs规范的内置变量.如果使用了esm 是不会自动注入这个变量的. 在commonjs中,注入了__dirname,__filename, module, export ...
- Linux搭建ESP-IDF开发环境
下载esp-gitee-tools git clone git@gitee.com:EspressifSystems/esp-gitee-tools.git 替换github网址 cd esp-git ...
- java 项目的创建
新建一个 Spring Initializr
- Vue——方法(methods)
我们用 methods 选项向组件实例添加方法,它应该是一个包含所需方法的对象: <div id="app"></div> <script> c ...
- FFmpeg中的关键方法及结构体(二)avformat_open_input
1.avformat_open_input 该方法声明在libavformat/avformat.h:2093 int avformat_open_input(AVFormatContext **ps ...