#P6303. Prefix Swap to Achieve Uniform Strings

    ID: 19521 Type: Default 1000ms 256MiB

Prefix Swap to Achieve Uniform Strings

Prefix Swap to Achieve Uniform Strings

You are given two strings s and t consisting only of the characters a and b. You are allowed to perform the following operation as many times as you wish:

Select a prefix of s and a prefix of t (each prefix can be empty or the entire string) and swap them.

Your task is to obtain an operation sequence such that after all operations, one of the strings contains only the character a and the other contains only the character b. Although you should aim to use as few operations as possible, a non‐optimal solution may still earn partial points.

Note: An empty string is not considered valid as a final result. Also, if it is impossible to achieve the goal, you should output -1.

Hint (non-optimal solution): One simple approach is to check whether the input is already in one of the desired configurations, that is, either s consists solely of a and t solely of b, or s consists solely of b and t solely of a. In these cases you can output 0 (or, if needed, perform one swap of the entire strings in the second case to get the correct order). For all other cases, you may output -1 as a placeholder solution.

For example:

  • If s = "aaa" and t = "bbb", the output is 0.
  • If s = "bbb" and t = "aaa", note that this configuration is acceptable since one string contains only b and the other only a.
  • Otherwise, if the strings are mixed (for example, s = "abba" and t = "baba"), your solution may output -1 (this would be a non‐optimal partial solution).

inputFormat

The input consists of two lines. The first line contains the string s. The second line contains the string t. Both strings consist only of the characters a and b.

outputFormat

If it is possible to achieve the goal, first output an integer k indicating the number of operations, followed by k lines each containing two non‐negative integers. The i-th line represents an operation where you swap the prefix of s of length xi and the prefix of t of length yi. (Note that a prefix length of 0 means an empty prefix.) If no operations are needed, output 0. If it is impossible to achieve the goal, output a single line with -1.

sample

aaa
bbb
0