QOJ.ac

QOJ

時間限制: 1 s 記憶體限制: 512 MB 總分: 100

#12970. 交换游戏

统计

去年你发明了一种可以用棋盘和骰子玩的游戏,今年你又发明了一种可以用单个字符串玩的新游戏。

游戏开始时有一个包含 $N$ 个小写英文字母('a' 到 'z')的字符串,你可以交换该字符串中的任意两个字符,这个步骤可以执行零次或多次。你的目标是达到字典序最小的字符串。

但最终字符串有一些限制。对于每个位置,它必须是给定字母中的一个(给定字母不一定对每个位置都相同)。例如,第一个位置必须是 'a' 或 'b',第二个位置必须是 'b' 或 'c',依此类推。

注意,这些限制仅针对最终字符串,这意味着你可以进行一些操作,使字符串暂时变为无效,经过后续操作后再变为有效字符串。

给定初始字符串和每个位置的限制,你的任务是编写一个程序,找出经过零次或多次操作后能得到的字典序最小的有效字符串。

注:当比较两个长度相同的不同字符串时,字典序较小的字符串是指在第一个不同的位置上字母较小的那个字符串。

输入格式

输入包含一个或多个测试用例。

输入的第一行是一个整数 $T$,表示测试用例的数量($1 \le T \le 100$)。

接下来是测试用例,每个测试用例的第一行包含一个初始字符串 $S$,由 $N$ 个小写英文字母组成($1 \le N \le 100$)。

接下来的 $N$ 行中,每行包含一个字符串 $C_i$,由 $L_i$ 个互不相同的小写英文字母组成($1 \le L_i \le 5$),表示最终字符串中第 $i$ 个位置的有效字母。每个 $C_i$ 中的每个字母在 $S$ 中至少出现一次。

输出格式

对于每个测试用例,单行输出经过零次或多次操作后能得到的字典序最小的有效字符串。如果不存在这样的有效字符串,则输出 NO SOLUTION(不含引号)。

样例

输入格式 1

2
abcde
abcde
a
abcde
abcde
abcde
abcde
ab
ab
ab
abcde
abcde

输出格式 1

bacde
NO SOLUTION

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.