A. You Are Given Two Binary Strings...
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given two binary strings xx and yy, which are binary representations of some two integers (let's denote these integers as f(x)f(x) and f(y)f(y)). You can choose any integer k≥0k≥0, calculate the expression sk=f(x)+f(y)⋅2ksk=f(x)+f(y)⋅2k and write the binary representation of sksk in reverse order (let's denote it as revkrevk). For example, let x=1010x=1010 and y=11y=11; you've chosen k=1k=1 and, since 21=10221=102, so sk=10102+112⋅102=100002sk=10102+112⋅102=100002 and revk=00001revk=00001.
For given xx and yy, you need to choose such kk that revkrevk is lexicographically minimal (read notes if you don't know what does "lexicographically" means).
It's guaranteed that, with given constraints, kk exists and is finite.
Input
The first line contains a single integer TT (1≤T≤1001≤T≤100) — the number of queries.
Next 2T2T lines contain a description of queries: two lines per query. The first line contains one binary string xx, consisting of no more than 105105 characters. Each character is either 0 or 1.
The second line contains one binary string yy, consisting of no more than 105105 characters. Each character is either 0 or 1.
It's guaranteed, that 1≤f(y)≤f(x)1≤f(y)≤f(x) (where f(x)f(x) is the integer represented by xx, and f(y)f(y) is the integer represented by yy), both representations don't have any leading zeroes, the total length of xx over all queries doesn't exceed 105105, and the total length of yy over all queries doesn't exceed 105105.
Output
Print TT integers (one per query). For each query print such kk that revkrevk is lexicographically minimal.
Example
input
Copy
4 1010 11 10001 110 1 1 1010101010101 11110000
output
Copy
1 3 0 0
Note
The first query was described in the legend.
In the second query, it's optimal to choose k=3k=3. The 23=1000223=10002 so s3=100012+1102⋅10002=10001+110000=1000001s3=100012+1102⋅10002=10001+110000=1000001and rev3=1000001rev3=1000001. For example, if k=0k=0, then s0=10111s0=10111 and rev0=11101rev0=11101, but rev3=1000001rev3=1000001 is lexicographically smaller than rev0=11101rev0=11101.
In the third query s0=10s0=10 and rev0=01rev0=01. For example, s2=101s2=101 and rev2=101rev2=101. And 0101 is lexicographically smaller than 101101.
The quote from Wikipedia: "To determine which of two strings of characters comes when arranging in lexicographical order, their first letters are compared. If they differ, then the string whose first letter comes earlier in the alphabet comes before the other string. If the first letters are the same, then the second letters are compared, and so on. If a position is reached where one string has no more letters to compare while the other does, then the first (shorter) string is deemed to come first in alphabetical order."
题意:给你两个零一串(是二进制数),a,b,和待求值 k 问你怎样使a + b * 2 ^ k的反向后的字典序最小。
思路:看题面还是个高质量水题,但把住出题人想考的东西后还是很容易想到思路的。对于普通的加法我们如果希望这个二进制串的反向的字典序较小的话,我们肯定是希望他们的低位有更多的零,而这两个串产生更多零的条件必须是有两个相同为一的位相加,那么如果这两个数相加后不能产生更多的零了呢?这个时候我们用到第二个题干,2 ^ k ,事实上用笔算一下,b * 2 ^ k相当于将 b 右移了 k 个单位,这样我们就可以达到自动控制按位加这两个数,那怎么加呢?直接找到b串的lowbit然后在a中高位寻找(题干已给a >= b),然后将这个位移距离输出就好了,还有更好的选择吗?没有,因为我们必须处理这个低位的一,不然,后面就算得到了很多很多零,这个一还是会直接影响大小。
void init_cin()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
}
int main()
{
init_cin();
char s1[100001] , s2[100001];
int n;
cin >> n;
while(n--)
{
cin >> s1 >> s2;
int begin = 0;
int len1 = strlen(s1) , len2 = strlen(s2);
for(int i = len2 - 1 ; i >= 0 ; i --) if(s2[i] == '1') {begin = i;break;}
int dx = len2 - begin;
int cnt = 0;
for(int i = len1 - dx ; i >= 0 ; -- i,cnt++)
if(s1[i] == '1') break;
cout << cnt << endl;
}
}