CF BUDDY
← Problems·

1705D · Mark and Lightbulbs

1800 · combinatorics, constructive algorithms, greedy

Problem: Mark has just purchased a rack of nn lightbulbs. The state of the lightbulbs can be described with binary string s=s1s2sns = s_1s_2\dots s_n, where si=1s_i=\texttt{1} means that the ii-th lightbulb is turned on, while si=0s_i=\texttt{0} means that the ii-th lightbulb is turned off.

Unfortunately, the lightbulbs are broken, and the only operation he can perform to change the state of the lightbulbs is the following:

  • Select an index ii from 2,3,,n12,3,\dots,n-1 such that si1si+1s_{i-1}\ne s_{i+1}.
  • Toggle sis_i. Namely, if sis_i is 0\texttt{0}, set sis_i to 1\texttt{1} or vice versa.

Mark wants the state of the lightbulbs to be another binary string tt. Help Mark determine the minimum number of operations to do so.

Input Format: The first line of the input contains a single integer qq (1q1041\leq q\leq 10^4) — the number of test cases.

The first line of each test case contains a single integer nn (3n21053\leq n\leq 2\cdot 10^5) — the number of lightbulbs.

The second line of each test case contains a binary string ss of length nn — the initial state of the lightbulbs.

The third line of each test case contains a binary string tt of length nn — the final state of the lightbulbs.

It is guaranteed that the sum of nn across all test cases does not exceed 21052\cdot 10^5.

Output Format: For each test case, print a line containing the minimum number of operations Mark needs to perform to transform ss to tt. If there is no such sequence of operations, print 1-1.

Note: In the first test case, one sequence of operations that achieves the minimum number of operations is the following.

  • Select i=3i=3, changing 0100\texttt{01}\color{red}{\texttt{0}}\texttt{0} to 0110\texttt{01}\color{red}{\texttt{1}}\texttt{0}.
  • Select i=2i=2, changing 0110\texttt{0}\color{red}{\texttt{1}}\texttt{10} to 0010\texttt{0}\color{red}{\texttt{0}}\texttt{10}.

In the third test case, even though the first digits of ss and tt are the same and the last digits of ss and tt are the same, it can be shown that there is no sequence of operations that satisfies the condition.

In the fourth test case, one sequence that achieves the minimum number of operations is the following:

  • Select i=3i=3, changing 000101\texttt{00}\color{red}{\texttt{0}}\texttt{101} to 001101\texttt{00}\color{red}{\texttt{1}}\texttt{101}.
  • Select i=2i=2, changing 001101\texttt{0}\color{red}{\texttt{0}}\texttt{1101} to 011101\texttt{0}\color{red}{\texttt{1}}\texttt{1101}.
  • Select i=4i=4, changing 011101\texttt{011}\color{red}{\texttt{1}}\texttt{01} to 011001\texttt{011}\color{red}{\texttt{0}}\texttt{01}.
  • Select i=5i=5, changing 011001\texttt{0110}\color{red}{\texttt{0}}\texttt{1} to 011011\texttt{0110}\color{red}{\texttt{1}}\texttt{1}.
  • Select i=3i=3, changing 011011\texttt{01}\color{red}{\texttt{1}}\texttt{011} to 010011\texttt{01}\color{red}{\texttt{0}}\texttt{011}.

Sample Cases

Case 1

Input

4
4
0100
0010
4
1010
0100
5
01001
00011
6
000101
010011

Output

2
-1
-1
5

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback