CF1009G.Allowed Letters

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Polycarp has just launched his new startup idea. The niche is pretty free and the key vector of development sounds really promising, so he easily found himself some investors ready to sponsor the company. However, he is yet to name the startup!

Actually, Polycarp has already came up with the name but some improvement to it will never hurt. So now he wants to swap letters at some positions in it to obtain the better name. It isn't necessary for letters to be adjacent.

In addition, each of the investors has chosen some index in the name and selected a set of letters that can go there. Indices chosen by different investors are pairwise distinct. If some indices aren't chosen by any investor then any letter can go there.

Finally, Polycarp is sure that the smallest lexicographically name is the best. (Like why do you think Google decided to become Alphabet?)

More formally, you are given a string consisting of lowercase Latin letters from "a" to "f". You can swap letters at any positions arbitrary number of times (zero swaps is also possible).

What is the smallest lexicographically name you can obtain such that the letter at every position is among the allowed letters?

If Polycarp can't produce any valid name then print "Impossible".

波利卡普刚刚启动了他的新创业项目。这个领域目前竞争者较少,而关键的发展方向听起来非常有前景,因此他轻松地找到了一些愿意为公司提供资金支持的投资者。然而,他尚未为这家初创公司命名!

实际上,波利卡普已经想好了一个名字,但对其进行一些优化总归是无害的。因此,他现在希望通过对名字中某些位置上的字母进行交换,来获得一个更优的名字。(交换的位置不必相邻。)

此外,每位投资者都选择了一个名字中的特定下标,并指定了该位置上允许出现的字母集合。不同投资者所选的下标两两互不相同。若某些下标未被任何投资者选定,则这些位置上可以放置任意字母。

最后,波利卡普确信:字典序最小的名字才是最优的。(比如,你是否想过为什么谷歌决定更名为 Alphabet?)

更形式化地说,你将得到一个仅由小写拉丁字母 "a" 到 "f" 组成的字符串。你可以对字符串中任意位置上的字母执行任意次数的交换(包括零次交换)。

在满足“每个位置上的字母均属于其对应允许字母集合”的前提下,你能得到的字典序最小的名字是什么?

如果波利卡普无法构造出任何一个合法的名字,则输出 "Impossible"。

输入格式

The first line is the string ss (1≤∣s∣≤1051 \le |s| \le 10^5) — the name Polycarp has came up with. The string consists only of lowercase Latin letters from "a" to "f".

The second line contains a single integer mm (0≤m≤∣s∣0 \le m \le |s|) — the number of investors.

The ii-th of the next mm lines contain an integer number posipos_i and a non-empty string of allowed characters for posipos_i (1≤posi≤∣s∣1 \le pos_i \le |s|). Each string contains pairwise distinct letters from "a" to "f". pos1,pos2,…,posmpos_1, pos_2, \dots, pos_m are pairwise distinct. If any position of the string doesn't appear in the investors demands then any letter can go in this position.

第一行是字符串 ss(1≤∣s∣≤1051 \le |s| \le 10^5)——Polycarp 想出的名字。该字符串仅由小写拉丁字母 "a" 到 "f" 组成。

第二行包含一个整数 mm(0≤m≤∣s∣0 \le m \le |s|)——投资者的数量。

接下来的 mm 行中,第 ii 行包含一个整数 posipos_i 和一个非空字符串,表示位置 posipos_i 上允许使用的字符(1≤posi≤∣s∣1 \le pos_i \le |s|)。每个字符串均由 "a" 到 "f" 中互不相同的字母组成。pos1,pos2,…,posmpos_1, pos_2, \dots, pos_m 两两不同。若字符串的某个位置未在投资者的需求中出现,则该位置可填入任意字母。

输出格式

If Polycarp can't produce any valid name then print "Impossible".

Otherwise print the smallest lexicographically name Polycarp can obtain by swapping letters in string ss such that the letter at every position is among the allowed ones.

如果 Polycarp 无法生成任何有效名称,则输出 "Impossible"。

否则,输出 Polycarp 通过对字符串 ss 中的字母进行交换所能得到的字典序最小的有效名称,要求每个位置上的字母均属于该位置允许的字母集合。

输入输出样例

  • 输入#1

    bedefead
    5
    2 e
    1 dc
    5 b
    7 ef
    6 ef

    输出#1

    deadbeef
  • 输入#2

    abacaba
    0

    输出#2

    aaaabbc
  • 输入#3

    fc
    2
    1 cfab
    2 f

    输出#3

    cf

输入解题思路,AI测评打分。不知道怎么写?

首页