公式

E - Swap 0^X and 1^Y 解説 by evima


If the number of 0 and 1 in \(S\) and \(T\) differ, it is clearly impossible to transform \(S\) into \(T\). Below, assume that \(S\) and \(T\) both contain the same number of 0 and 1.

For a string \(U\) consisting only of 0 and 1, take each maximal run of 0\(^l\) and replace it with the concatenation of \(\lfloor l/X \rfloor\) occurrences of A and \((l \bmod X)\) occurrences of 0, and do the same for each maximal run of 1\(^l\), replacing it with the concatenation of \(\lfloor l/Y \rfloor\) occurrences of B and \((l \bmod Y)\) occurrences of 1. Call the resulting string the compressed string of \(U\).

\(U\) can be transformed into \(U'\) by the operations described in the statement if and only if the compressed string of \(U\) can be transformed into the compressed string of \(U'\) by repeating the following three basic operations:

  • Swap adjacent 0 and A.
  • Swap adjacent 1 and B.
  • Swap adjacent A and B.

(Notice in particular that even if we repeat these basic operations on the compressed string, we will not create runs of \(X\) consecutive 0 or \(Y\) consecutive 1 at any point, because 0 only swaps with A, and 1 only swaps with B.)

Therefore, the problem is “Can the compressed string of \(S\) be transformed into the compressed string of \(T\) by repeating the above basic operations?” For simplicity, let us still call these compressed strings of \(S\) and \(T\) simply \(S\) and \(T\).

In these basic operations, 0 and 1 cannot be swapped, nor can 0 and B, nor can 1 and A. Hence, the following invariants [A], [B], [C] remain unchanged under any sequence of these operations:

  • [A] The subsequence obtained by extracting all 0 and 1 in order (without changing their relative positions).
  • [B] For each pair of consecutive 0s (and at the ends of the string), the number of B in between them.
  • [C] For each pair of consecutive 1s (and at the ends of the string), the number of A in between them.

Thus, for \(S\) to be transformable into \(T\), at the very least, [A], [B], and [C] must match between \(S\) and \(T\).

Furthermore, we show that if these invariants match, that is sufficient to guarantee that \(S\) can be transformed into \(T\).

First, take \(S\) and repeatedly perform as many as possible of the following three operations (in any order) on it, ending up with a string \(S'\):

  • Replace a contiguous substring 0A with A0.
  • Replace a contiguous substring B1 with 1B.
  • Replace a contiguous substring BA with AB.

Since the number of subsequence occurrences of 0A, 1B, BA decreases with each operation, after a finite number of operations, we will end up with a string \(S'\) where no more operations can be done.

Applying the same process to \(T\) yields \(T'\).

We will then show that if [A], [B], and [C] of \(S\) and \(T\) coincide, then regardless of how we perform the operations above, we have \(S' = T'\). Once this is shown, because the basic operations are reversible, it is also possible to transform \(T'\) back into \(T\). Thus, \(S \to S' = T' \to T\) is a valid transformation, completing the proof.

To show this, note that each of \(S'\) and \(T'\) obtained above satisfies the following:

\((\star)\) It does not contain 0A, B1, or BA as a contiguous substring.

Thus, it suffices to show that the string \(S'\) satisfying the condition \((\star)\) is unique given \(S\) and the invariants [A], [B], and [C].

Let \(R\) be the string consisting of 0 and 1 mentioned in [A]. \(S'\) must be a string that can be obtained by inserting A and B between the characters of \(R\) or at the ends of \(R\).

Now, since \(S'\) does not contain 0A as a contiguous substring, A can only be inserted immediately after 1 (or at the beginning of the string). Additionally, the specified invariant [C] uniquely determines the number of A to insert immediately after each 1 in \(R\) (and at the beginning of the string). Similarly, B can only be inserted immediately before 1 (or at the end of the string), and the specified invariant [B] uniquely determines the number of B to insert at each of those positions.

Moreover, in any position between characters of \(R\) (or at the beginning or the end of the string) where both A and B are inserted, the order of those A and B is uniquely determined: all A must appear to the left of all B, because the string \(S'\) must not contain BA.

Thus, for a given \(S\), there is exactly one string \(S'\) that shares the same invariants [A], [B], and [C] and satisfies \((\star)\).

投稿日時:
最終更新: