#P12353. Minimum Operations to Transform Binary String

    ID: 14453 Type: Default 1000ms 256MiB

Minimum Operations to Transform Binary String

Minimum Operations to Transform Binary String

You are given two binary strings (S) and (T) containing characters in ({0,1}). The string (S) has length (N) and the string (T) has length (N+1). You are allowed to perform operations on (T) in order to transform it into (S).

There are two types of operations you can perform on (T):

  1. Deletion Operation: Choose an index (1 \leq i \leq |T|) and delete the character (T_i). All characters to the right shift one position to the left.

  2. Flipping Operation: Choose two indices (1\leq l\leq r\leq |T|). For every index (i) such that (l \leq i \leq r) and ((l+i)\equiv 0 \pmod{2}), update (T_i \gets T_i \oplus 1) (i.e. flip the bit).

    Your goal is to transform (T) into (S) with the minimum number of operations. Note that you are only required to output the minimum number of operations (you do not need to provide an explicit sequence of moves).

    Explanation of the Flipping Operation:
    For a chosen segment ([l,r]), note that the condition ((l+i)\equiv 0 \pmod{2}) is equivalent to requiring (i \equiv l \pmod{2}). In other words, if you choose a segment starting at (l), then all positions in that segment which have the same parity as (l) will have their bits flipped.

    Hint: Since (|T| = |S| + 1), you must perform exactly one deletion operation. After the deletion, let (U) be the resulting string. Then, you can think of the flipping operations as independently correcting bits in the positions with odd indices and even indices. For each parity group, you need one operation per contiguous block of indices where (U) differs from (S). The answer is the minimum, over all possible single-character deletions in (T), of (1 + \text{(number of flipping operations required)}).

inputFormat

The input consists of three lines:

  • The first line contains a single integer (N), the length of string (S).
  • The second line contains the binary string (S) of length (N).
  • The third line contains the binary string (T) of length (N+1).

outputFormat

Output a single integer — the minimum number of operations required to transform (T) into (S).

sample

3
101
1101
1