AtCoder Regular Contest++ 216
A
黑白棋 3
Reversi 3
AtCoder
⌀ 1708
原题
←B →

黑白棋 3AI (CI翻译)

Problem Statement

题目描述

AI (CI翻译)

You are given strings $A$ and $B$ of length $N$ consisting of 0 and 1. Let $A_i$ denote the $i$-th character of $A$.

给定两个由 0 和 1 组成、长度均为 $N$ 的字符串 $A$ 和 $B$。令 $A_i$ 表示 $A$ 的第 $i$ 个字符。AI (CI翻译)

You can perform the following operation any number of times, possibly zero.

你可以进行任意次以下操作,也可以一次都不进行。AI (CI翻译)

  • Choose an integer $i\ (2 \leq i \leq N-1)$ satisfying $A_{i-1} = A_{i+1}$, and flip $A_i$ (change 1 to 0 or 0 to 1).
  • 选择一个满足 $A_{i-1} = A_{i+1}$ 的整数 $i\ (2 \leq i \leq N-1)$,并翻转 $A_i$(将 1 变为 0,或将 0 变为 1)。
AI (CI翻译)

Determine whether you can make $A$ equal to $B$ by repeating the operation, and if so, find the minimum number of operations required.

判断能否通过重复操作使 $A$ 与 $B$ 相等;如果可以,求所需的最少操作次数。AI (CI翻译)

You are given $T$ test cases; solve each of them.

给定 $T$ 组测试数据,请分别求解。AI (CI翻译)

Constraints

约束条件

AI (CI翻译)
  • $1 \leq T \leq 2 \times 10^5$
  • $3 \leq N \leq 10^6$
  • $A$ and $B$ are strings of length $N$ consisting of 0 and 1.
  • $T$ and $N$ are integers.
  • The sum of $N$ over all test cases is at most $10^6$.
  • $1 \leq T \leq 2 \times 10^5$
  • $3 \leq N \leq 10^6$
  • $A$ 和 $B$ 是由 0 和 1 组成、长度均为 $N$ 的字符串。
  • $T$ 和 $N$ 均为整数。
  • 所有测试数据中 $N$ 的总和不超过 $10^6$。
AI (CI翻译)

Input

输入格式

AI (CI翻译)

The input is given from Standard Input in the following format:

输入从标准输入按以下格式给出:AI (CI翻译)

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
AI (CI翻译)

Each test case is given in the following format:

每组测试数据按以下格式给出:AI (CI翻译)

N
A
B

N
A
B
AI (CI翻译)

Output

输出格式

AI (CI翻译)

Output the answers for $\mathrm{case}_1, \mathrm{case}_2, \ldots, \mathrm{case}_T$ in the following format.

按以下格式输出 $\mathrm{case}_1, \mathrm{case}_2, \ldots, \mathrm{case}_T$ 的答案。AI (CI翻译)

If it is impossible to make $A$ equal to $B$, print -1.

如果无法使 $A$ 与 $B$ 相等,输出 -1。AI (CI翻译)

If it is possible, print the minimum number of operations required.

如果可以,输出所需的最少操作次数。AI (CI翻译)

样例

Input #1
4
4
0001
0111
6
101101
011100
5
10101
10101
10
0101000101
0011100111
Output #1
2
-1
0
6
For the first test case, you can make $A$ equal to $B$ in two operations as follows. 1. Choose $i = 2$. $A$ becomes `0101`. 2. Choose $i = 3$. $A$ becomes `0111`. For the second test case, it is impossible to make $A$ equal to $B$ no matter how you operate.
题目信息
题号
arc216_a
时间限制
2 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。