QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 1024 MB Total points: 100

#14851. 决斗排序

Statistics

我们将使用一种基于马栗树(horse chestnut tree)自然本能的新颖算法来重新组织单词中的字母:在其组成字母之间进行一场盛大的锦标赛。

我们将挑选几对字母并让它们进行对决。这些字母之间的比赛结果决定了它们的相对顺序。例如,如果 $a < b$,那么在一个组织良好的单词中,所有 $a$ 的出现都应该在该单词中所有 $b$ 的出现之前。如果既没有 $a < b$ 约束,也没有 $b < a$ 约束,那么字母 $a$ 和 $b$ 可以以任何顺序相互交错。

给定一个单词以及若干此类比赛结果,请确定该单词的一种合理重新排列,以满足所有给定的约束条件。可能存在多个这样的重新排列,也可能根本不存在。

输入格式

  • 第一行包含一个整数 $n$ ($1 \le n \le 700$),表示需要遵守的规则数量。
  • 接下来的 $n$ 行,每行包含两个不同的小写字母 $a$ 和 $b$ 的一个互不相同的顺序关系,由字符 <> 以及空格分隔。
  • 最后一行包含待处理的单词 $s$ ($1 \le |s| \le 100\,000$)。

输出格式

输出该单词排序后的版本,如果无法同时满足所有规则对单词进行排序,则输出 IMPOSSIBLE

如果存在多个答案,您可以输出其中任意一个。

样例

输入样例 1

3
m > i
n < i
i > o
minion

输出样例 1

noniim

输入样例 2

1
b < n
banana

输出样例 2

banana

输入样例 3

6
b < a
a > b
n < b
a > b
n < b
a > b
bananaman

输出样例 3

nnnbaaama

输入样例 4

2
a < b
b < a
unsolvable

输出样例 4

IMPOSSIBLE

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.