atcoder#ARC157F. [ARC157F] XY Ladder LCS
[ARC157F] XY Ladder LCS
Score : points
Problem Statement
You are given strings and of length consisting of X
and Y
.
For each , you can swap the -th character of and the -th character of or choose not to do it.
There are pairs of strings that can result from this process, including duplicates. Find the longest string that is a common subsequence (not necessarily contiguous) of one of such pairs.
If there are multiple such strings, find the lexicographically smallest of them.
What is a common subsequence?
A subsequence of string S is a string obtained by deleting zero or more characters from S and concatenating the remaining characters without changing the order. A common subsequence of strings S and T is a string that is a subsequence of both S and T. (See also Sample Output 1.)Constraints
- Each of and is a string of length consisting of
X
andY
.
Input
The input is given from Standard Input in the following format:
Output
Print the lexicographically smallest of the longest strings that can be a common subsequence of the resulting pair of strings.
3
XXX
YYY
XY
- If you swap nothing, the only common subsequence of
XXX
andYYY
is the empty string. - If you swap the -st characters, the common subsequences of
YXX
andXYY
are the empty string,X
, andY
. - If you swap the -nd characters, the common subsequences of
XYX
andYXY
are the empty string,X
,Y
,XY
, andYX
. - If you swap the -rd characters, the common subsequences of
XXY
andYYX
are the empty string,X
andY
.
Doing two or more swaps is equivalent to one of the above after swapping and themselves.
Thus, the longest strings that can be a common subsequence are XY
and YX
. The lexicographically smaller of them, XY
, is the answer.
1
X
Y
The answer may be the empty string.
4
XXYX
YYYY
XYY
XYY
will be a common subsequence after, for instance, swapping just the -nd characters.
Any string that is longer or has the same length and is lexicographically smaller will not be a common subsequence after any combination of swaps, so this is the answer.