#abc326d. D - ABC Puzzle

D - ABC Puzzle

Score : 450450 points

问题描述

你将获得一个整数 NN 和两个长度为 NN 的字符串 RRCC,由字符 ABC 组成。请解决以下问题。

存在一个 N×NN \times N 的网格,所有单元格最初为空。
你可以在每个单元格中最多写入一个来自 ABC 的字符。(也可以选择让单元格保持为空。)

判断是否可能满足以下所有条件,如果可能,则输出一种满足条件的方法。

  • 每行和每列恰好包含一个 A、一个 B 和一个 C
  • ii 行最左边书写的字符与字符串 RR 的第 ii 个字符相匹配。
  • ii 列顶部书写的字符与字符串 CC 的第 ii 个字符相匹配。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

You are given an integer NN and strings RR and CC of length NN consisting of A, B, and C. Solve the following problem.

There is a N×NN \times N grid. All cells are initially empty.
You can write at most one character from A, B, and C in each cell. (You can also leave the cell empty.)

Determine if it is possible to satisfy all of the following conditions, and if it is possible, print one way to do so.

  • Each row and each column contain exactly one A, one B, and one C.
  • The leftmost character written in the ii-th row matches the ii-th character of RR.
  • The topmost character written in the ii-th column matches the ii-th character of CC.

Constraints

  • NN is an integer between 33 and 55, inclusive.
  • RR and CC are strings of length NN consisting of A, B, and C.

Input

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

NN

RR

CC

Output

If there is no way to fill the grid to satisfy the conditions in the problem statement, print No in one line.

Otherwise, print one such way to fill the grid in the following format:

Yes

A1A_1

A2A_2

\vdots

ANA_N

The first line should contain Yes. The ii-th of the subsequent NN lines should contain a string AiA_i of length NN.

  • If the jj-th character of AiA_i is ., it indicates that the cell in the ii-th row from the top and the jj-th column from the left is empty.

  • If the jj-th character of AiA_i is A, it indicates that A is written in the cell in the ii-th row from the top and the jj-th column from the left.

  • If the jj-th character of AiA_i is B, it indicates that B is written in the cell in the ii-th row from the top and the jj-th column from the left.

  • If the jj-th character of AiA_i is C, it indicates that C is written in the cell in the ii-th row from the top and the jj-th column from the left.

If there are multiple correct ways to fill the grid, you may print any of them.

Sample Input 1

5
ABCBC
ACAAB

Sample Output 1

Yes
AC..B
.BA.C
C.BA.
BA.C.
..CBA

The grid in the output example satisfies all the following conditions, so it will be treated as correct.

  • Each row contains exactly one A, one B, and one C.
  • Each column contains exactly one A, one B, and one C.
  • The leftmost characters written in the rows are A, B, C, B, C from top to bottom.
  • The topmost characters written in the columns are A, C, A, A, B from left to right.

Sample Input 2

3
AAA
BBB

Sample Output 2

No

For this input, there is no way to fill the grid to satisfy the conditions.

update @ 2024/3/10 01:49:29