A. You Are Given Two Binary Strings…
A. You Are Given Two Binary Strings…
You are given two binary strings x and y, which are binary representations of some two integers (let’s denote these integers as f(x) and f(y)). You can choose any integer k≥0, calculate the expression sk=f(x)+f(y)⋅2k and write the binary representation of sk in reverse order (let’s denote it as revk). For example, let x=1010 and y=11; you’ve chosen k=1 and, since 21=102, so sk=10102+112⋅102=100002 and revk=00001.
For given x and y, you need to choose such k that revk is lexicographically minimal (read notes if you don’t know what does “lexicographically” means).
It’s guaranteed that, with given constraints, k exists and is finite.
Input
The first line contains a single integer T (1≤T≤100) — the number of queries.
Next 2T lines contain a description of queries: two lines per query. The first line contains one binary string x, consisting of no more than 105 characters. Each character is either 0 or 1.
The second line contains one binary string y, consisting of no more than 105 characters. Each character is either 0 or 1.
It’s guaranteed, that 1≤f(y)≤f(x) (where f(x) is the integer represented by x, and f(y) is the integer represented by y), both representations don’t have any leading zeroes, the total length of x over all queries doesn’t exceed 105, and the total length of y over all queries doesn’t exceed 105.
Output
Print T integers (one per query). For each query print such k that revk is lexicographically minimal.
Example
input
4
1010
11
10001
110
1
1
1010101010101
11110000
output
1
3
0
0
题意:给你两个二进制数字x,y,让你选择一个数字k,存在公式 sk = x + y * 2^k ,定义revk是sk的倒序,求能 使得revk字典序最小 的k值。
大佬的思路:https://blog.csdn.net/weixin_43334251/article/details/98940717
题目乍一看非常难懂的亚子,其实2^k有妙用,从二进制的角度来看,其实你选的k值是多少,就是让y左移多少位罢了。然后我们可以发现,其实只要让y串最后一个1(从左往右数),去对准x串中最近的一个1就好了。下面举一个例子:
输入x为10001, y为110,

我们可以看到y的最后一个1对准了x的0,由于我们的y串只能左移,也就是在后面加0。而y串最后一个1距离x串最近的一个1还差3距离,所以我们把y串左移3位,也就是k值取3可以得到下图:
这个时候通过公式得到的revk就是最小字典序了。
#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
#include<math.h>
#include<string>
#include<string.h>
#include<vector>
#include<utility>
#include<map>
#include<queue>
#include<set>
#define mx 0x3f3f3f3f
#define ll long long
using namespace std;
string s1,s2;
int main()
{
int n;
cin>>n;
while(n--)
{
cin>>s1>>s2;
int len1=s1.length();
int len2=s2.length();
int pos=;
for(int i=len2-;s2[i];i--)
{
pos++;
if(s2[i]=='')
break;
}
int cnt=;
for(int i=len1-pos;s1[i];i--)
{
if(s1[i]=='')
break;
cnt++;
}
cout<<cnt<<endl;
}
return ;
}
A. You Are Given Two Binary Strings…的更多相关文章
- [CC-BSTRLCP]Count Binary Strings
[CC-BSTRLCP]Count Binary Strings 题目大意: 对于一个长度为\(n\)的\(\texttt0/\texttt1\)串\(S\),如果存在一个切分\(i\),使得\(S_ ...
- Binary Strings Gym - 101161G 矩阵快速幂 + 打表
http://codeforces.com/gym/101161/attachments 这题通过打表,可以知道长度是i的时候的合法方案数. 然后得到f[1] = 2, f[2] = 3, f[3] ...
- codeforces gym #101161G - Binary Strings(矩阵快速幂,前缀斐波那契)
题目链接: http://codeforces.com/gym/101161/attachments 题意: $T$组数据 每组数据包含$L,R,K$ 计算$\sum_{k|n}^{}F(n)$ 定义 ...
- [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings
这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...
- [LeetCode] Add Binary 二进制数相加
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- 【leetcode】Add Binary
题目简述: Given two binary strings, return their sum (also a binary string). For example, a = "11&q ...
- leetcode解题:Add binary问题
顺便把之前做过的一个简单难度的题也贴上来吧 67. Add Binary Given two binary strings, return their sum (also a binary strin ...
- Leetcode Add Binary
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- [LintCode] Add Binary 二进制数相加
Given two binary strings, return their sum (also a binary string). Have you met this question in a r ...
随机推荐
- C语言-switch语句的使用。对文件的输出处理。for循环和if的结合使用。
//函数fun功能:统计字符串中各元音字母的个数,注意:不区分大小写. //重难点:switch语句的使用. #include <stdlib.h> #include <conio. ...
- win10中,vscode安装go插件排雷指南
最近学习go,想着使用强大的vscode编写go,在安装go插件过程中,遇到了很多问题.下面记录解决方案. 1)win10环境,安装go,vscode,git 配置GOPATH环境变量,在我的电脑-& ...
- Git 安装配置及工作流程
在使用Git前我们需要先安装 Git.Git 目前支持 Linux/Unix.Solaris.Mac和 Windows 平台上运行. Git 各平台安装包下载地址为:http://git-scm.co ...
- python导入openpyxl报错问题,终于解决啦
问题:折腾了一上午,安装.卸载openpyxl多次,cmd中明明显示安装成功,可python文件import时就是报错 1.安装openpyxl后,python文件导入一直报错,经过一上午的努力,终于 ...
- 同步I/O和异步I/O
同步I/O包括:阻塞,非阻塞,多路复用 阻塞模型:给你送的外卖到了,给你打电话,你不去取,外卖小哥一直在那等你,直到你来,形成阻塞,当然应该给外卖小哥点赞,哈哈哈哈!! 非阻塞模型:取外卖的主人非常饿 ...
- Fluent_Python_Part4面向对象,11-iface-abc,协议(接口),抽象基类
第四部分第11章,接口:从协议到抽象基类(重点讲抽象基类) 接口就是实现特定角色的方法集合. 严格来说,协议是非正式的接口(只由文档约束),正式接口会施加限制(抽象基类对接口一致性的强制). 在Pyt ...
- java 事务解释。
面试的时候,面试人员总喜欢问在spring中, 1. 如果一个主线程上有一个事务,在事务中开启了一个线程.子线程跑出异常,对主线程有没有影响,或者主线程产生异常对子线程有没有影响. 这个时候,你只要记 ...
- Springboot学习:Web开发介绍
简介 使用SpringBoot: 1).创建SpringBoot应用,选中我们需要的模块: 2).SpringBoot已经默认将这些场景配置好了,只需要在配置文件中指定少量配置就可以运行起来 3).自 ...
- Pychram 运行程序在 run 窗口和 python console 窗口之间切换
有图有真相 第一步: 第二步:
- 一、json与jsonp的使用
1.json与jsonp的引入 在ajax中 JSON用来解决数据交换问题,而JSONP来实现跨域. 备注:跨域也可以通过服务器端代理来解决; 理解:JSON是一种数据交换格式,而J ...