AtCoder Grand Contest 019
D
Shift and Flip
AtCoder
⌀ 2601
原题
← CE →
翻译中…

Problem Statement

翻译中…

You have two strings $A = A_1 A_2 ... A_n$ and $B = B_1 B_2 ... B_n$ of the same length consisting of $0$ and $1$.

翻译中…

You can transform $A$ using the following operations in any order and as many times as you want:

翻译中…
  • Shift $A$ by one character to the left (i.e., if $A = A_1 A_2 ... A_n$, replace $A$ with $A_2 A_3 ... A_n A_1$).
  • Shift $A$ by one character to the right (i.e., if $A = A_1 A_2 ... A_n$, replace $A$ with $A_n A_1 A_2 ... A_{n-1}$).
  • Choose any $i$ such that $B_i = 1$. Flip $A_i$ (i.e., set $A_i = 1 - A_i$).
翻译中…

You goal is to make strings $A$ and $B$ equal.

翻译中…

Print the smallest number of operations required to achieve this, or $-1$ if the goal is unreachable.

翻译中…

Constraints

翻译中…
  • $1 \leq |A| = |B| \leq 2,000$
  • $A$ and $B$ consist of $0$ and $1$.
翻译中…

Input

翻译中…

Input is given from Standard Input in the following format:

翻译中…

A
B

翻译中…

Output

翻译中…

Print the smallest number of operations required to make strings $A$ and $B$ equal, or $-1$ if the goal is unreachable.

翻译中…

样例

Input #1
1010
1100
Output #1
3
Here is one fastest way to achieve the goal: - Flip $A_1$: $A = 0010$ - Shift $A$ to the left: $A = 0100$ - Flip $A_1$ again: $A = 1100$
Input #2
1
0
Output #2
-1
There is no way to flip the only bit in $A$.
Input #3
11010
10001
Output #3
4
Here is one fastest way to achieve the goal: - Shift $A$ to the right: $A = 01101$ - Flip $A_5$: $A = 01100$ - Shift $A$ to the left: $A = 11000$ - Shift $A$ to the left again: $A = 10001$
Input #4
0100100
1111111
Output #4
5
Flip $A_1$, $A_3$, $A_4$, $A_6$ and $A_7$ in any order.
题目信息
题号
agc019_d
时间限制
2 s
内存限制
256 MB

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