A - XXYYX

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

X, Y からなる長さ N の文字列 S であって,以下の条件を満たすものが存在するかどうかを判定してください.

条件: S 中で 2 つの文字が隣り合う (N - 1) 箇所のうち

  • ちょうど A 個が XX
  • ちょうど B 個が XY
  • ちょうど C 個が YX
  • ちょうど D 個が YY である.

制約

  • 1 \leq N \leq 2 \times 10^5
  • A \geq 0
  • B \geq 0
  • C \geq 0
  • D \geq 0
  • A + B + C + D = N - 1

入力

入力は以下の形式で標準入力から与えられる.

N A B C D

出力

条件を満たす文字列 S が存在するなら Yes を,存在しないなら No を出力せよ.


入力例 1

5 1 1 1 1

出力例 1

Yes

たとえば S = {}XXYYX とすると,2 つの文字が隣り合う箇所は左から順に XX, XY, YY, YX であり,各 1 個ずつとなって条件を満たします.


入力例 2

5 1 2 1 0

出力例 2

Yes

たとえば S = {}XXYXY が条件を満たします.


入力例 3

5 0 4 0 0

出力例 3

No

条件を満たす文字列は存在しません.

Score : 300 points

Problem Statement

Determine whether there is a string S of length N consisting of X and Y that satisfies the following condition.

Condition: Among the (N - 1) pairs of consecutive characters in S,

  • exactly A are XX,
  • exactly B are XY,
  • exactly C are YX, and
  • exactly D are YY.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • A \geq 0
  • B \geq 0
  • C \geq 0
  • D \geq 0
  • A + B + C + D = N - 1

Input

The input is given from Standard Input in the following format:

N A B C D

Output

If there is a string S that satisfies the condition, print Yes; otherwise, print No.


Sample Input 1

5 1 1 1 1

Sample Output 1

Yes

For instance, if S = {}XXYYX, the pairs of consecutive characters are XX, XY, YY, and YX from left to right. Each pattern occurs exactly once, so the condition is satisfied.


Sample Input 2

5 1 2 1 0

Sample Output 2

Yes

For instance, S = {}XXYXY satisfies the condition.


Sample Input 3

5 0 4 0 0

Sample Output 3

No

No string satisfies the condition.

B - XYYYX

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

X, Y からなる長さ N の文字列 S が与えられます. S 中の相異なる位置にある K 文字を選び,選んだ文字が X であれば Y に,Y であれば X にそれぞれ置き換えます. 置き換えた後の文字列中で Y 同士が隣り合う箇所は最大でいくつになるかを求めてください.

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq K \leq N
  • SX, Y からなる長さ N の文字列である.

入力

入力は以下の形式で標準入力から与えられる.

N K
S

出力

置き換えた後の文字列中で Y 同士が隣り合う箇所の個数の最大値を出力せよ.


入力例 1

5 1
XYXYX

出力例 1

2

選ぶのは 1 文字だけです.

  • 1 文字目を選ぶと,置き換えた後の文字列は YYXYX となり,1, 2 文字目の 1 箇所で Y 同士が隣り合っています.
  • 2 文字目を選ぶと,置き換えた後の文字列は XXXYX となり,Y 同士が隣り合っている箇所はありません.
  • 3 文字目を選ぶと,置き換えた後の文字列は XYYYX となり,2, 3 文字目と 3, 4 文字目の 2 箇所で Y 同士が隣り合っています.
  • 4 文字目を選ぶと,置き換えた後の文字列は XYXXX となり,Y 同士が隣り合っている箇所はありません.
  • 5 文字目を選ぶと,置き換えた後の文字列は XYXYY となり,4, 5 文字目の 1 箇所で Y 同士が隣り合っています.

以上より,求める最大値は 2 です.


入力例 2

5 4
XYXYX

出力例 2

2

1, 2, 3, 5 文字目を選んで YXYYY とするか,1, 3, 4, 5 文字目を選んで YYYXY とするのが最適です. 同じ位置にある文字を複数回選ぶことはできないことに注意してください.

Score : 500 points

Problem Statement

You are given a string S of length N consisting of X and Y. You will choose K characters at distinct positions in S and change each of them: X becomes Y and Y becomes X. Find the maximum possible number of pairs of consecutive Ys in the resulting string.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq K \leq N
  • S is a string of length N consisting of X and Y.

Input

The input is given from Standard Input in the following format:

N K
S

Output

Print the maximum possible number of pairs of consecutive Ys in the resulting string.


Sample Input 1

5 1
XYXYX

Sample Output 1

2

You will choose one character.

  • If you choose the 1-st character, the resulting string is YYXYX, with one pair of consecutive Ys at positions 1, 2.
  • If you choose the 2-nd character, the resulting string is XXXYX, with no pair of consecutive Ys.
  • If you choose the 3-rd character, the resulting string is XYYYX, with two pairs of consecutive Ys at positions 2, 3 and 3, 4.
  • If you choose the 4-th character, the resulting string is XYXXX, with no pair of consecutive Ys.
  • If you choose the 5-th character, the resulting string is XYXYY, with one pair of consecutive Ys at positions 4, 5.

Thus, the sought maximum number is 2.


Sample Input 2

5 4
XYXYX

Sample Output 2

2

It is optimal to choose the 1-st, 2-nd, 3-rd, and 5-th characters to get YXYYY, or choose the 1-st, 3-rd, 4-th, and 5-th characters to get YYYXY. Note that you may not choose a character at the same position multiple times.

C - YY Square

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 600

問題文

HW 列のマス目の各マスに X, Y のいずれかの文字が書かれています. 上から i 行目,左から j 列目のマスを (i, j) で表します. マス目に書かれている文字は H 個の文字列 S_1, S_2, \dots, S_H によって与えられ,S_ij 文字目がマス (i, j) に書かれた文字を表します.

下または右に隣接するマスへの移動を繰り返してマス (1, 1) からマス (H, W) に至る経路 P に対して,

  • P で通るマスに書かれた文字を順に並べて得られる長さ (H + W - 1) の文字列」を \mathrm{str}(P) とし,
  • \mathrm{str}(P) 中で Y 同士が隣り合う箇所の個数の 2」を Pスコアと定義します.

そのような経路 P としてあり得るものは \displaystyle\binom{H + W - 2}{H - 1} 通りありますが,その全てに対するスコアの総和を 998244353 で割った余りを求めてください.

\binom{N}{K} の意味 \displaystyle\binom{N}{K} は,N 個の相異なる要素から K 個を選ぶ場合の数を表す二項係数です.

制約

  • 1 \leq H \leq 2000
  • 1 \leq W \leq 2000
  • S_i \ (1 \leq i \leq H)X, Y からなる長さ W の文字列である.

入力

入力は以下の形式で標準入力から与えられる.

H W
S_1
S_2
\vdots
S_H

出力

あり得る経路全てに対するスコアの総和を 998244353 で割った余りを出力せよ.


入力例 1

2 2
YY
XY

出力例 1

4

経路 P としてあり得るものは (1, 1) \to (1, 2) \to (2, 2)(1, 1) \to (2, 1) \to (2, 2)2 通りです.

  • (1, 1) \to (1, 2) \to (2, 2) の場合,\mathrm{str}(P) = {}YYY であり,1, 2 文字目と 2, 3 文字目の 2 箇所で Y 同士が隣り合っているので,スコアは 2^2 = 4 です.
  • (1, 1) \to (2, 1) \to (2, 2) の場合,\mathrm{str}(P) = {}YXY であり,Y 同士が隣り合う箇所は無いので,スコアは 0^2 = 0 です.

したがって,求める総和は 4 + 0 = 4 となります.


入力例 2

2 2
XY
YY

出力例 2

2

2 通りのいずれの経路の場合も \mathrm{str}(P) = {}XYY であり,スコアは 1^2 = 1 です.


入力例 3

10 20
YYYYYYYYYYYYYYYYYYYY
YYYYYYYYYYYYYYYYYYYY
YYYYYYYYYYYYYYYYYYYY
YYYYYYYYYYYYYYYYYYYY
YYYYYYYYYYYYYYYYYYYY
YYYYYYYYYYYYYYYYYYYY
YYYYYYYYYYYYYYYYYYYY
YYYYYYYYYYYYYYYYYYYY
YYYYYYYYYYYYYYYYYYYY
YYYYYYYYYYYYYYYYYYYY

出力例 3

423787835

スコアの総和を 998244353 で割った余りを出力してください.

Score : 600 points

Problem Statement

There is a grid with H rows and W columns where each square has one of the characters X and Y written on it. Let (i, j) denote the square at the i-th row from the top and j-th column from the left. The characters on the grid are given as H strings S_1, S_2, \dots, S_H: the j-th character of S_i is the character on square (i, j).

For a path P from square (1, 1) to square (H, W) obtained by repeatedly moving down or right to the adjacent square, the score is defined as follows.

  • Let \mathrm{str}(P) be the string of length (H + W - 1) obtained by concatenating the characters on the squares visited by P in the order they are visited.
  • The score of P is the square of the number of pairs of consecutive Ys in \mathrm{str}(P).

There are \displaystyle\binom{H + W - 2}{H - 1} such paths. Find the sum of the scores over all those paths, modulo 998244353.

What is \binom{N}{K}? \displaystyle\binom{N}{K} is the binomial coefficient representing the number of ways to choose K from N distinct elements.

Constraints

  • 1 \leq H \leq 2000
  • 1 \leq W \leq 2000
  • S_i \ (1 \leq i \leq H) is a string of length W consisting of X and Y.

Input

The input is given from Standard Input in the following format:

H W
S_1
S_2
\vdots
S_H

Output

Print the sum of the scores over all possible paths, modulo 998244353.


Sample Input 1

2 2
YY
XY

Sample Output 1

4

There are two possible paths P: (1, 1) \to (1, 2) \to (2, 2) and (1, 1) \to (2, 1) \to (2, 2).

  • For (1, 1) \to (1, 2) \to (2, 2), we have \mathrm{str}(P) = {}YYY, with two pairs of consecutive Ys at positions 1, 2 and 2, 3, so the score is 2^2 = 4.
  • For (1, 1) \to (2, 1) \to (2, 2), we have \mathrm{str}(P) = {}YXY, with no pairs of consecutive Ys , so the score is 0^2 = 0.

Thus, the sought sum is 4 + 0 = 4.


Sample Input 2

2 2
XY
YY

Sample Output 2

2

For either of the two possible paths P, we have \mathrm{str}(P) = {}XYY, for a score of 1^2 = 1.


Sample Input 3

10 20
YYYYYYYYYYYYYYYYYYYY
YYYYYYYYYYYYYYYYYYYY
YYYYYYYYYYYYYYYYYYYY
YYYYYYYYYYYYYYYYYYYY
YYYYYYYYYYYYYYYYYYYY
YYYYYYYYYYYYYYYYYYYY
YYYYYYYYYYYYYYYYYYYY
YYYYYYYYYYYYYYYYYYYY
YYYYYYYYYYYYYYYYYYYY
YYYYYYYYYYYYYYYYYYYY

Sample Output 3

423787835

Print the sum of the scores modulo 998244353.

D - YY Garden

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 600

問題文

HW 列のマス目の各マスに X, Y のいずれかの文字が書かれています. 上から i 行目,左から j 列目のマスを (i, j) で表します. マス目に書かれている文字は H 個の文字列 S_1, S_2, \dots, S_H によって与えられ,S_ij 文字目がマス (i, j) に書かれた文字を表します.

隣り合う各行および各列の間に,マス目全体を横断(縦断)するように柵を設置できます. 柵同士は交差しても構いません. 柵の設置後に,「あるマスから始めて上下左右に隣接するマスへの移動を繰り返すことで,柵を越えずに到達可能なマス全体」を区画と定義します. (出力例 1 の説明も参考にしてください.)

柵の設置方法は全部で 2^{H-1} \times 2^{W-1} 通りありますが,そのうち次の条件を満たすものの個数を 998244353 で割った余りを求めてください.

条件: 各区画には Y が書かれたマスがちょうど 2 個含まれている.

制約

  • 1 \leq H \leq 2000
  • 1 \leq W \leq 2000
  • S_i \ (1 \leq i \leq H)X, Y からなる長さ W の文字列である.

入力

入力は以下の形式で標準入力から与えられる.

H W
S_1
S_2
\vdots
S_H

出力

条件を満たす柵の設置方法の総数を 998244353 で割った余りを出力せよ.


入力例 1

2 3
XYY
YXY

出力例 1

2

柵の設置方法として,以下の 8 通りがあります.

X Y Y    X|Y Y    X Y|Y    X|Y|Y
          |          |      | |
Y X Y    Y|X Y    Y X|Y    Y|X|Y


X Y Y    X|Y Y    X Y|Y    X|Y|Y
-----    -+---    ---+-    -+-+-
Y X Y    Y|X Y    Y X|Y    Y|X|Y

たとえば,2, 3 列目の間に柵を設置した場合,区画は

XY
YX
Y
Y

であり,それぞれにちょうど 2 個の Y が含まれているので,条件を満たします.

また,1, 2 行目の間と 1, 2 列目の間に柵を設置した場合,区画は

X
YY
Y
XY

となり,2 つ目の区画以外にはちょうど 2 個の Y が含まれていないので,条件を満たしません.


入力例 2

2 3
XYX
YYY

出力例 2

0

どのように柵を設置しても条件を満たしません.


入力例 3

2 58
YXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXY
YXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXY

出力例 3

164036797

条件を満たす柵の設置方法の総数を 998244353 で割った余りを出力してください.

Score : 600 points

Problem Statement

There is a grid with H rows and W columns where each square has one of the characters X and Y written on it. Let (i, j) denote the square at the i-th row from the top and j-th column from the left. The characters on the grid are given as H strings S_1, S_2, \dots, S_H: the j-th character of S_i is the character on square (i, j).

You can set up fences across the grid between adjacent rows and columns. Fences may intersect. After setting up fences, a block is defined as the set of squares reachable from some square by repeatedly moving up, down, left, or right to the adjacent square without crossing fences. (See also Sample Output 1.)

There are 2^{H-1} \times 2^{W-1} ways to set up fences. How many of them satisfy the following condition, modulo 998244353?

Condition: Each block has exactly two squares with Y written on it.

Constraints

  • 1 \leq H \leq 2000
  • 1 \leq W \leq 2000
  • S_i \ (1 \leq i \leq H) is a string of length W consisting of X and Y.

Input

The input is given from Standard Input in the following format:

H W
S_1
S_2
\vdots
S_H

Output

Print the number of ways to set up fences to satisfy the condition, modulo 998244353.


Sample Input 1

2 3
XYY
YXY

Sample Output 1

2

Below are the eight ways to set up fences.

X Y Y    X|Y Y    X Y|Y    X|Y|Y
          |          |      | |
Y X Y    Y|X Y    Y X|Y    Y|X|Y


X Y Y    X|Y Y    X Y|Y    X|Y|Y
-----    -+---    ---+-    -+-+-
Y X Y    Y|X Y    Y X|Y    Y|X|Y

For instance, if a fence is set up between the 2-nd and 3-rd columns, the blocks are:

XY
YX
Y
Y

Each of these blocks has exactly two Ys, so the condition is satisfied.

If a fence is set up between the 1-st and 2-nd rows and the 1-st and 2-nd columns, the blocks are:

X
YY
Y
XY

These blocks, except the second, do not have exactly two Ys, so the condition is not satisfied.


Sample Input 2

2 3
XYX
YYY

Sample Output 2

0

There is no way to set up fences to satisfy the condition.


Sample Input 3

2 58
YXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXY
YXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXYXXY

Sample Output 3

164036797

Print the number of ways modulo 998244353.

E - XXYX Binary Tree

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 700

問題文

N 頂点の根付き木が与えられます. 頂点には 1 から N の相異なる整数の番号が付いており,根は頂点 1 です. 根以外の各頂点 i の親は頂点 P_i であり,根を含む各頂点は,子を持たないか,ちょうど 2 個の子を持つかのいずれかです.

与えられた木の各頂点に X, Y のいずれかの文字を書き込んで,以下の条件を満たすことが可能かどうかを判定してください.

条件: 木の各辺に関して,両端点に書き込まれた文字を親 P_i から子 i に向かう順に並べて得られる長さ 2 の文字列を考える. そのような文字列はのべ (N - 1) 個あるが,そのうち

  • ちょうど A 個が XX
  • ちょうど B 個が XY
  • ちょうど C 個が YX であり,
  • YY は存在しない.

T 個のテストケースが与えられるので,それぞれについて答えてください.

制約

  • T \geq 1
  • N \geq 1
  • 1 つの入力に含まれるテストケースについて,N の総和は 10^4 以下である.
  • A \geq 0
  • B \geq 0
  • C \geq 0
  • A + B + C = N - 1
  • 1 \leq P_i < i \ \ (2 \leq i \leq N)
  • 各頂点 k \ (1 \leq k \leq N) は親 P_i \ (2 \leq i \leq N) として合計 0 回または 2現れる.

入力

入力は以下の形式で標準入力から与えられる.

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

各テストケース \mathrm{case}_i \ (1 \leq i \leq T) は以下の形式である.

N A B C
P_2 P_3 \cdots P_N

出力

T 行出力せよ. i 行目には,i 番目のテストケースについて,条件を満たす文字の書き込み方が存在するなら Yes を,存在しないなら No を出力せよ.


入力例 1

3
7 2 2 2
1 1 2 2 3 3
7 0 2 4
1 1 2 2 3 3
7 2 0 4
1 1 2 2 4 4

出力例 1

Yes
Yes
No

1 番目のテストケースについて,たとえば頂点 1 から 7 の順に XXYXYXX と書き込めば,

  • (1, 2) で得られる文字列は XX
  • (1, 3) で得られる文字列は XY
  • (2, 4) で得られる文字列は XX
  • (2, 5) で得られる文字列は XY
  • (3, 6) で得られる文字列は YX
  • (3, 7) で得られる文字列は YX

であり,XX, XY, YX がそれぞれ 2 個ずつとなって条件を満たします.

2 番目のテストケースについて,たとえば XYYXXXX と書き込めば条件を満たします.

3 番目のテストケースについては,どのように書き込んでも条件を満たしません.

Score : 700 points

Problem Statement

You are given a rooted tree with N vertices. The vertices are numbered 1 to N, and the root is vertex 1. The parent of each vertex i except the root is vertex P_i. Each vertex, including the root, has no child or exactly two children.

Determine whether it is possible to write one of the characters X and Y on each vertex of the given tree to satisfy the following condition.

Condition: For each edge of the tree, consider the string of length 2 obtained by concatenating the characters written on the endpoints in the order from the parent P_i to the child i. Among the (N - 1) strings obtained in this way,

  • exactly A are XX,
  • exactly B are XY,
  • exactly C are YX, and
  • none is YY.

You have T test cases to solve.

Constraints

  • T \geq 1
  • N \geq 1
  • For each input, the sum of N over the test cases is at most 10^4.
  • A \geq 0
  • B \geq 0
  • C \geq 0
  • A + B + C = N - 1
  • 1 \leq P_i < i \ \ (2 \leq i \leq N)
  • Each vertex k \ (1 \leq k \leq N) occurs as a parent P_i \ (2 \leq i \leq N) zero times or twice in total.

Input

The input is given from Standard Input in the following format:

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

Each test case \mathrm{case}_i \ (1 \leq i \leq T) is in the following format:

N A B C
P_2 P_3 \cdots P_N

Output

Print T lines. The i-th line should contain Yes if there is a way to write characters to satisfy the condition, and No otherwise.


Sample Input 1

3
7 2 2 2
1 1 2 2 3 3
7 0 2 4
1 1 2 2 3 3
7 2 0 4
1 1 2 2 4 4

Sample Output 1

Yes
Yes
No

For the first test case, if you, for instance, write XXYXYXX in this order on vertices 1 to 7,

  • from the edge (1, 2), we obtain XX,
  • from the edge (1, 3), we obtain XY,
  • from the edge (2, 4), we obtain XX,
  • from the edge (2, 5), we obtain XY,
  • from the edge (3, 6), we obtain YX, and
  • from the edge (3, 7), we obtain YX.

Each of XX, XY, and YX occurs twice, so the condition is satisfied.

For the second case, one way to satisfy the condition is to write XYYXXXX.

For the third case, there is no way to satisfy the condition.

F - XY Ladder LCS

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 900

問題文

X, Y からなる長さ N の文字列 S, T が与えられます. 各 i = 1, 2, \dots, N に関して,Si 文字目と Ti 文字目を入れ替えるかどうかを自由に選べます. このとき,結果として得られる文字列の組はのべ 2^N 通りありますが,そのいずれかの共通部分列(連続とは限らない)となる文字列のうち最長のものを求めてください. ただし,そのような文字列が複数ある場合,そのうち辞書順で最初に現れるものを求めてください.

共通部分列とは 文字列 S部分列とは,S から 0 個以上の文字を削除して,残った文字を元の順で並べて得られる文字列のことをいいます. 文字列 S, T共通部分列とは,ST のどちらの部分列でもあるような文字列のことをいいます. (出力例 1 の説明も参考にしてください.)

制約

  • 1 \leq N \leq 50
  • S, T はそれぞれ X, Y からなる長さ N の文字列である.

入力

入力は以下の形式で標準入力から与えられる.

N
S
T

出力

入れ替え後の文字列の組の共通部分列としてあり得る最長の文字列のうち,辞書順で最初に現れるものを出力せよ.


入力例 1

3
XXX
YYY

出力例 1

XY
  • どの文字も入れ替えない場合,XXXYYY の共通部分列は空文字列のみです.
  • 1 文字目だけを入れ替えた場合,YXXXYY の共通部分列は,空文字列, X, Y です.
  • 2 文字目だけを入れ替えた場合,XYXYXY の共通部分列は,空文字列, X, Y, XY, YX です.
  • 3 文字目だけを入れ替えた場合,XXYYYX の共通部分列は,空文字列, X, Y です.

2 文字以上の入れ替えは,ST 自体を入れ替えて考えれば上記のいずれかに該当します. したがって,共通部分列としてあり得る最長の文字列は XYYX であり,辞書順で最初に現れる XY が答えとなります.


入力例 2

1
X
Y

出力例 2


答えが空文字列となることもあります.


入力例 3

4
XXYX
YYYY

出力例 3

XYY

たとえば 2 文字目だけを入れ替えた場合に XYY が共通部分列となります. より長い文字列,あるいは,同じ長さであって辞書順でより早く現れる文字列は,どのように入れ替えたとしても共通部分列にはなり得ないため,これが答えとなります.

Score : 900 points

Problem Statement

You are given strings S and T of length N consisting of X and Y. For each i = 1, 2, \dots, N, you can swap the i-th character of S and the i-th character of T or choose not to do it. There are 2^N 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

  • 1 \leq N \leq 50
  • Each of S and T is a string of length N consisting of X and Y.

Input

The input is given from Standard Input in the following format:

N
S
T

Output

Print the lexicographically smallest of the longest strings that can be a common subsequence of the resulting pair of strings.


Sample Input 1

3
XXX
YYY

Sample Output 1

XY
  • If you swap nothing, the only common subsequence of XXX and YYY is the empty string.
  • If you swap the 1-st characters, the common subsequences of YXX and XYY are the empty string, X, and Y.
  • If you swap the 2-nd characters, the common subsequences of XYX and YXY are the empty string, X, Y, XY, and YX.
  • If you swap the 3-rd characters, the common subsequences of XXY and YYX are the empty string, X and Y.

Doing two or more swaps is equivalent to one of the above after swapping S and T themselves. Thus, the longest strings that can be a common subsequence are XY and YX. The lexicographically smaller of them, XY, is the answer.


Sample Input 2

1
X
Y

Sample Output 2


The answer may be the empty string.


Sample Input 3

4
XXYX
YYYY

Sample Output 3

XYY

XYY will be a common subsequence after, for instance, swapping just the 2-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.