#P6303. Prefix Swap to Achieve Uniform Strings
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"
andt = "bbb"
, the output is0
. - If
s = "bbb"
andt = "aaa"
, note that this configuration is acceptable since one string contains onlyb
and the other onlya
. - Otherwise, if the strings are mixed (for example,
s = "abba"
andt = "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