AtCoder Regular Contest 119
B
Electric Board
AtCoder
⌀ 1196
原题
← AC →
翻译中…

Problem Statement

翻译中…

An electric bulletin board is showing a string $S$ of length $N$ consisting of 0 and 1.

翻译中…

You can do the following operation any number of times, where $S_i$ denotes the $i$-th character $(1 \leq i \leq N)$ of the string shown in the board.

翻译中…

Operation: choose a pair of integers $(l, r)$ $(1 \leq l < r \leq N)$ satisfying one of the conditions below, and swap $S_l$ and $S_r$.

  • $S_l=$ 0 and $S_{l+1}=\cdots=S_r=$ 1.
  • $S_{l}=\cdots=S_{r-1}=$ 1 and $S_r=$ 0.
翻译中…

Determine whether it is possible to make the string shown in the board match $T$, and find the minimum number of operations needed if it is possible.

翻译中…

Constraints

翻译中…
  • $2 \leq N \leq 500000$
  • $S$ is a string of length $N$ consisting of 0 and 1.
  • $T$ is a string of length $N$ consisting of 0 and 1.
翻译中…

Input

翻译中…

Input is given from Standard Input in the following format:

翻译中…

N
S
T

翻译中…

Output

翻译中…

If it is impossible to make the board show the string $T$, print -1.

翻译中…

If it is possible, find the minimum number of operations needed.

翻译中…

样例

Input #1
7
1110110
1010111
Output #1
2
Here is one possible way to make the board show the string `1010111` in two operations: - Do the operation with $(l, r) = (2, 4)$, changing the string in the board from `1110110` to `1011110`. - Do the operation with $(l, r) = (4, 7)$, changing the string in the board from `1011110` to `1010111`.
Input #2
20
11111000000000011111
11111000000000011111
Output #2
0
The board already shows the string $T$ before doing any operation, so the answer is $0$.
Input #3
6
111100
111000
Output #3
-1
If there is no sequence of operations that makes the board show the string $T$, print `-1`.
Input #4
119
10101111011101001011111000111111101011110011010111111111111111010111111111111110111111110111110111101111111111110111011
11111111111111111111111111011111101011111011110111110010100101001110111011110111111111110010011111101111111101110111011
Output #4
22
题目信息
题号
arc119_b
时间限制
2 s
内存限制
1024 MB

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