IAEPC Preliminary Contest (Codeforces Round 999, Div. 1 + Div. 2)
F1
Kevin and Binary String (Easy Version)
Codeforces
2100
原题
← EF2 →
翻译中…

This is the easy version of the problem. The difference between the versions is that in this version, string $t$ consists of only '0' and '1'. You can hack only if you solved all versions of this problem.

翻译中…

Kevin has a binary string $s$ of length $n$. Kevin can perform the following operation:

翻译中…
  • Choose two adjacent blocks of $s$ and swap them.
翻译中…

A block is a maximal substring$^{\text{∗}}$ of identical characters. Formally, denote $s[l,r]$ as the substring $s_l s_{l+1} \ldots s_r$. A block is $s[l,r]$ satisfying:

翻译中…
  • $l=1$ or $s_l\not=s_{l-1}$.
  • $s_l=s_{l+1} = \ldots = s_{r}$.
  • $r=n$ or $s_r\not=s_{r+1}$.
翻译中…

Adjacent blocks are two blocks $s[l_1,r_1]$ and $s[l_2,r_2]$ satisfying $r_1+1=l_2$.

翻译中…

For example, if $s=\mathtt{000}\,\mathbf{11}\,\mathbf{00}\,\mathtt{111}$, Kevin can choose the two blocks $s[4,5]$ and $s[6,7]$ and swap them, transforming $s$ into $\mathtt{000}\,\mathbf{00}\,\mathbf{11}\,\mathtt{111}$.

翻译中…

Given a string $t$ of length $n$ consisting of '0', '1' and '?', Kevin wants to determine the minimum number of operations required to perform such that for any index $i$ ($1\le i\le n$), if $t_i\not=$ '?' then $s_i=t_i$. If it is impossible, output $-1$.

翻译中…

$^{\text{∗}}$A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by the deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

翻译中…

The first line of each test case contains a string $s$ consisting of '0' and '1'.

翻译中…

The second line of each test case contains a string $t$ consisting of '0' and '1'.

翻译中…

It is guaranteed that the lengths of $s$ and $t$ are the same.

翻译中…

It is guaranteed that the sum of the length of $s$ over all test cases will not exceed $4\cdot 10^5$.

翻译中…

Output

翻译中…

For each test case, output one integer — the minimum number of operations required. If it is impossible, output $-1$.

翻译中…

Note

翻译中…

In the first test case, the possible way is shown in the statement.

翻译中…

In the second test case, one possible way could be:

翻译中…
  • Swap blocks $[2, 2], [3, 3]$, $s$ will become $\mathtt{001101}$.
  • Swap blocks $[3, 4], [5, 5]$, $s$ will become $\mathtt{000111}$.
  • Swap blocks $[1, 3], [4, 6]$, $s$ will become $\mathtt{111000}$.
翻译中…

样例

Input #1
6
0001100111
0000011111
010101
111000
0101
0110
0101
1010
011001
001110
0
1
Output #1
1
3
1
-1
-1
-1
InputCopy OutputCopy
题目信息
题号
2061F1
时间限制
2 s
内存限制
256 MB
标签
贪心
模拟

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。