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…的更多相关文章

  1. [CC-BSTRLCP]Count Binary Strings

    [CC-BSTRLCP]Count Binary Strings 题目大意: 对于一个长度为\(n\)的\(\texttt0/\texttt1\)串\(S\),如果存在一个切分\(i\),使得\(S_ ...

  2. Binary Strings Gym - 101161G 矩阵快速幂 + 打表

    http://codeforces.com/gym/101161/attachments 这题通过打表,可以知道长度是i的时候的合法方案数. 然后得到f[1] = 2, f[2] = 3, f[3] ...

  3. codeforces gym #101161G - Binary Strings(矩阵快速幂,前缀斐波那契)

    题目链接: http://codeforces.com/gym/101161/attachments 题意: $T$组数据 每组数据包含$L,R,K$ 计算$\sum_{k|n}^{}F(n)$ 定义 ...

  4. [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings

    这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...

  5. [LeetCode] Add Binary 二进制数相加

    Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...

  6. 【leetcode】Add Binary

    题目简述: Given two binary strings, return their sum (also a binary string). For example, a = "11&q ...

  7. leetcode解题:Add binary问题

    顺便把之前做过的一个简单难度的题也贴上来吧 67. Add Binary Given two binary strings, return their sum (also a binary strin ...

  8. Leetcode Add Binary

    Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...

  9. [LintCode] Add Binary 二进制数相加

    Given two binary strings, return their sum (also a binary string). Have you met this question in a r ...

随机推荐

  1. C语言:有序递增链表的插入问题。

    //已建立一个带头节点的单向链表,链表中的各结点按结点数据域中的数据递增有序连接.fun函数:把形参x的值放入一个新结点并插入链表中,使插入的各个数据域的数据仍保持递增有序. #include < ...

  2. Django 字段中 null 和 blank的区别

    blank 设置为 True 时,字段可以为空 设置为 False 时,字段是必须填写的 默认为 False null 设置为 True 时,数据库的字段允许为NULL,而且表单中的空值将会被存储为N ...

  3. Linux - Shebang(#!)

    1. Shebang这个符号通常在Unix系统的脚本中第一行开头中写到,它指明了执行这个脚本文件的解释程序.

  4. python开发基础作业01:模拟登陆系统

    随老男孩学习python mark 作业要求及提示:编写登录接口 ''' 练习程序:编写登录接口 1. 输入用户名和密码 2. 认证成功后显示欢迎信息 3. 输错三次后锁定 输入三次后退出,下次同样用 ...

  5. openfire 安装配置时出现The Openfire database schema does not appear to be installed. Follow the installati错误的解决方案

    最近再弄openfire用openLDAP整合,本来没整合的时候选的标准数据库没问题,但是现在用嵌入式数据库,就报错了,,, 报错原因:没有导入openfire的数据表 解放办法: 1.登陆数据库 , ...

  6. python 解释器声明 unix #!/usr/bin/env python

    python 解释器声明 unix #!/usr/bin/env python 1. 问题的开始在代码移植到unix平台后,部分应用在运行时报如下错误:subprocess.Popen():OSErr ...

  7. 实现简单HttpServer案例

    <html> <head> <title>第一个表单</title> </head> <body> <pre> me ...

  8. 解决el-input 触发不了键盘事件

    <el-input v-model="form.loginName" placeholder="账号" @keyup.enter="doLogi ...

  9. python3.8+PySimpleGUI+进度条代码大全

    1.python3.8+PySimpleGUI+进度条大全 2.效果图: 3.代码: #导出模块 import PySimpleGUI as sg import time import inspect ...

  10. 吴裕雄--天生自然Numpy库学习笔记:NumPy 广播(Broadcast)

    广播(Broadcast)是 numpy 对不同形状(shape)的数组进行数值计算的方式, 对数组的算术运算通常在相应的元素上进行. 如果两个数组 a 和 b 形状相同,即满足 a.shape == ...