A. You Are Given Two Binary Strings...(二进制思维题)

本文探讨了一种算法挑战,即寻找使两个二进制字符串组合并反转后的结果最小化的k值。通过定位第一个'1'并进行适当位移,确保了结果的最小化。

在这里插入图片描述
在这里插入图片描述
题意很简单就是给你x和y转成二进制:f(x)和g(y)然后求最小的k使得;ans=f(x)+g(y)*2^k的二进制结果反转后最小值;
很明显直接reverse然后找第二个对应的第一个串的第一个1;
为什么呢?
理由:
因为2^k二进制表示为:
在这里插入图片描述
然后根据二进制运算:这个相当于就是十进制里面的乘10;因为比如(11)2乘上2^k:
在这里插入图片描述
然后拿第一个案例举例:
1010翻转0101那么我需要把最高位也就是0101的从左边开始的第一个1给尽可能的往后进位(如果写过加法运算的应该可以很容易理解);但是有个问题就是a,b串的长度问题,没关系,只要b的长度对齐上去开始就行;也就是:
比如:在这里插入图片描述
那么我就让红色圈的1往后进位(因为reverse了的嘛);
所以我只需要找到b串的第一个1,然后左边不断补充0,知道能和他们最开始对应的长度的后面找到a串的第一个1就可以了;
这样就能保证最小了;
也就是这个意思:
在这里插入图片描述
所以AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
int main(){
	char a[maxn],b[maxn];
	ll T;
	scanf("%lld",&T);
	while(T--){
		  scanf("%s %s",a,b);
		  int len1=strlen(a);//翻转
		  int len2=strlen(b);
		  reverse(a,a+len1);
		  reverse(b,b+len2);
		  int bb;
		  for(int i=0;i<len2;i++){
		  	  if(b[i]=='1'){//找到b穿的第一个0
		  	  	  bb=i;break;
				}
		  }
		  ll ans=0;
		  for(int i=bb;i<len1;i++){//找到所对应长度的a串的第一个1,让它向后进位;
		  	  if(a[i]=='1')break;
		  	  ans++;
		  }
		  printf("%lld\n",ans);
	}
	return 0;
}
B. Like the Bitset time limit per test1 second memory limit per test256 megabytes You are given a binary string∗ s of length n , as well as an integer k . Aquawave wants to construct a permutation† p of length n , so that for each 1≤i≤n , where si=1 , the following holds: For each interval [l,r] (1≤l≤r≤n ) whose length is at least k (i.e. r−l+1≥k ), if it covers position i (i.e. l≤i≤r ), then the maximum element among pl,pl+1,…,pr is not pi . Note that there are no such constraints on indices with si=0 . You have to find such a permutation, or determine that such permutations do not exist. ∗ A binary string is a string where each character is either 0 or 1 . † A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array), and [1,3,4] is also not a permutation (n=3 but there is 4 in the array). Input Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104 ). The description of the test cases follows. The first line of each test case contains two integers n and k (1≤n≤2⋅105 , 1≤k≤n ) — the length of s and the integer in the statements. The second line contains the binary string s of length n (si=0 or 1 ). It is guaranteed that the sum of n over all test cases does not exceed 2⋅105 . Output For each test case: If there is at least one possible permutation: Print "YES" in the first line of output; Then, print n integers p1,p2,…,pn (1≤pi≤n , all pi -s are distinct) in the second line — the permutation you constructed. Otherwise, print "NO" in the single line of output. You can output the tokens in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses. If there are multiple answers, you may output any of them.
08-29
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值