QOJ.ac

QOJ

时间限制: 3 s 内存限制: 1024 MB 总分: 100

#2518. 益智游戏

统计

对于一个字符串 $S$,定义 $Adjacency(S)$ 为无序对 $(S[i], S[i+1])$ 的多重集,其中 $i = 1, 2, \dots, |S| - 1$;定义 $\Sigma(S)$ 为字符 $S[i]$ 的多重集,其中 $i = 1, 2, \dots, |S|$,这里 $|S|$ 是 $S$ 的长度,$S[i]$ 是 $S$ 的第 $i$ 个字符。例如,对于 $S = \text{ABADDADCAB}$,我们有 $Adjacency(S) = \{\text{AB, BA, AD, DD, DA, AD, DC, CA, AB}\} = \{\text{AB, AB, AB, AC, AD, AD, AD, CD, DD}\}$ 且 $\Sigma(S) = \{\text{A, A, A, A, B, B, C, D, D, D}\}$。

John 正在玩一个益智游戏,给定两个字符集为 $\{\text{A, B, C, D}\}$ 的字符串 $P$ 和 $Q$(满足 $|P| > |Q|$),目标是在 $Q$ 中插入一些字符以得到一个字符串 $Q'$,使得 $\Sigma(Q') = \Sigma(P)$ 且 $Adjacency(Q') = Adjacency(P)$。例如,给定 $P = \text{ABADCAB}$ 和 $Q = \text{CBB}$,通过在 $Q$ 中插入 A, D, A, A,我们可以得到字符串 $Q' = \text{ADCABAB}$。容易验证 $\Sigma(Q') = \Sigma(P) = \{\text{A, A, A, B, B, C, D}\}$ 且 $Adjacency(Q') = Adjacency(P) = \{\text{AB, AB, AB, AC, AD, CD}\}$。因此,$Q'$ 是 $P = \text{ABADCAB}$ 和 $Q = \text{CBB}$ 的一个解。作为另一个例子,对于 $P = \text{ABA}$ 和 $Q = \text{CB}$,则无解。

请编写一个程序来帮助 John。更具体地说,给定两个字符串 $P$ 和 $Q$,你的程序需要计算出一个字符串 $Q'$,使得 $Q'$ 是通过在 $Q$ 中插入若干字符得到的,且满足 $\Sigma(Q') = \Sigma(P)$ 以及 $Adjacency(Q') = Adjacency(P)$。

输入格式

输入的第一行是一个整数 $t$,表示测试用例的数量。每个测试用例包含三行:第一行给出两个整数,表示 $|P|$ 和 $|Q|$ 的长度;第二行给出字符串 $P$;第三行给出字符串 $Q$。

输出格式

对于每个测试用例,输出一个解字符串 $Q'$。如果存在多个解,你可以输出其中任意一个。如果无解,输出 "NO"。

数据范围

  • 测试用例数量最多为 15。
  • $P$ 的长度 $|P|$ 是一个介于 2 和 $10^3$ 之间的整数。
  • $Q$ 的长度 $|Q|$ 是一个介于 1 和 $10^3$ 之间的整数,且 $|P| - 18 \le |Q| \le |P| - 1$。
  • $P$ 和 $Q$ 均由字符 $\{\text{A, B, C, D}\}$ 组成。

样例

样例输入 1

3
7 3
ABADCAB
CBB
11 7
ABACCDBADAC
AADCDAC
3 2
ABA
CB

样例输出 1

ADCABAB
ABABDCCADAC
NO

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.