AtCoder Grand Contest 069
C
AB*A Changing
AtCoder
⌀ 3853
原题
← BD →
翻译中…

Problem Statement

翻译中…

You are given two strings $S$ and $T$ of length $N$ consisting of A and B. Let $s_i$ denote the $i$-th character of $S$.

翻译中…

You can perform the following operation on $S$ any number of times, possibly zero:

翻译中…
  • Choose an integer pair $(i, j)$ satisfying the following conditions:
  • $1 \leq i < j \leq N$
    • $s_i = s_j =$ A
    • $s_{i+1} = s_{i+2} = \ldots = s_{j-1} =$ B
  • Then, simultaneously replace each of $s_i, s_{i+1}, \ldots, s_j$ with the other character (A becomes B, and B becomes A).
翻译中…

Determine the minimum number of operations required to make $S$ equal to $T$. If it is impossible, print -1.

翻译中…

Constraints

翻译中…
  • $1 \leq N \leq 2 \times 10^5$
  • $S$ and $T$ are strings of length $N$ consisting of A and B.
翻译中…

Input

翻译中…

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

翻译中…

N
S
T

翻译中…

Output

翻译中…

If it is possible to make $S$ equal to $T$, print the minimum number of operations required; otherwise, output -1.

翻译中…

样例

Input #1
5
AAABA
BAAAB
Output #1
2
You can make $S$ equal to $T$ in two operations as follows: 1. Perform the operation with $(i, j) = (2, 3)$. $S$ becomes `ABBBA`. 2. Perform the operation with $(i, j) = (1, 5)$. $S$ becomes `BAAAB`. It is impossible to make $S$ equal to $T$ in fewer than two operations, so the answer is `2`.
Input #2
1
A
B
Output #2
-1
It is impossible to make $S$ equal to $T$. Note that you must choose $(i, j)$ with $i < j$.
Input #3
1
A
A
Output #3
0
Without any operation, $S$ is already equal to $T$.
Input #4
10
AAABBABAAB
BBABBAAABB
Output #4
7
题目信息
题号
agc069_c
时间限制
2 s
内存限制
2048 MB

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