CF1085E.Vasya and Templates

提高+/省选-

通过率:0%

时间限制:5.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Vasya owns three strings ss , aa and bb, each of them consists only of first kk Latin letters.

Let a template be such a string of length kk that each of the first kk Latin letters appears in it exactly once (thus there are k!k! distinct templates). Application of template pp to the string ss is the replacement of each character in string ss with pip_i, ii is the index of this letter in the alphabet. For example, applying template "bdca" to a string "aabccd" yields string "bbdcca".

Vasya wants to know if there exists such a template which yields a string lexicographically greater than or equal to string aa and lexicographically less than or equal to string bb after applying it to ss.

If there exist multiple suitable templates, print any of them.

String aa is lexicographically less than string bb if there is some ii (1≤i≤n1 \le i \le n) that ai<bia_i \lt b_i and for any jj (1≤j<i1 \le j \lt i) aj=bja_j = b_j.

You are required to answer tt testcases independently.

瓦西里拥有三个字符串 ss、aa 和 bb,它们均由前 kk 个拉丁字母组成。

我们称一个长度为 kk 的字符串为模板,当且仅当其中恰好包含前 kk 个拉丁字母各一次(因此共有 k!k! 个互不相同的模板)。将模板 pp 应用于字符串 ss,是指将 ss 中的每个字符替换为 pip_i,其中 ii 是该字符在字母表中的索引(即 'a' 对应 i=1i=1,'b' 对应 i=2i=2,依此类推)。例如,将模板 "bdca" 应用于字符串 "aabccd",得到字符串 "bbdcca"。

瓦西里想知道:是否存在某个模板,使得将它应用于 ss 后所得的字符串在字典序上大于等于字符串 aa,且小于等于字符串 bb。

若存在多个满足条件的模板,输出任意一个即可。

字符串 aa 在字典序上小于字符串 bb,当且仅当存在某个位置 ii(1≤i≤n1 \le i \le n),使得 ai<bia_i \lt b_i,且对所有 jj(1≤j<i1 \le j \lt i)均有 aj=bja_j = b_j。

你需要独立地回答 tt 组测试用例。

输入格式

The first line contains a single integer tt (1≤t≤1061 \le t \le 10^6) — the number of testcases.

In hacks you can only use t=1t = 1.

Each of the next tt lines contains the description of the testcase in the following form:

The first line of the testcase contains a single integer kk (1≤k≤261 \le k \le 26) — the length of the template.

The second line of the testcase contains the string ss (1≤∣s∣≤1061 \le |s| \le 10^6).

The third line of the testcase contains the string aa.

The fourth line of the testcase contains the string bb.

Strings ss, aa and bb have the same length (∣s∣=∣a∣=∣b∣|s| = |a| = |b|) and consist only of the first kk Latin letters, all letters are lowercase.

It is guaranteed that string aa is lexicographically less than or equal to string bb.

It is also guaranteed that the total length of strings over all testcase won't exceed 3⋅1063 \cdot 10^6.

第一行包含一个整数 tt(1≤t≤1061 \le t \le 10^6)—— 表示测试用例的数量。

在 hack 中,你只能使用 t=1t = 1。

接下来的 tt 行每行描述一个测试用例,格式如下:

每个测试用例的第一行包含一个整数 kk(1≤k≤261 \le k \le 26)—— 表示模板的长度。

每个测试用例的第二行包含字符串 ss(1≤∣s∣≤1061 \le |s| \le 10^6)。

每个测试用例的第三行包含字符串 aa。

每个测试用例的第四行包含字符串 bb。

字符串 ss、aa 和 bb 长度相同(即 ∣s∣=∣a∣=∣b∣|s| = |a| = |b|),且仅由前 kk 个拉丁字母组成,所有字母均为小写。

保证字符串 aa 的字典序小于或等于字符串 bb。

还保证所有测试用例中字符串的总长度不超过 3⋅1063 \cdot 10^6。

输出格式

Print the answers to all testcases in the following form:

If there exists no suitable template then print "NO" in the first line.

Otherwise print "YES" in the first line and the template itself in the second line (kk lowercase letters, each of the first kk Latin letters should appear exactly once).

If there exist multiple suitable templates, print any of them.

以如下格式输出所有测试用例的答案:

如果不存在合适的模板,则在第一行输出 "NO"。

否则,在第一行输出 "YES",并在第二行输出该模板(由 kk 个小写字母组成,前 kk 个拉丁字母应恰好各出现一次)。

如果存在多个合适的模板,输出其中任意一个即可。

输入输出样例

  • 输入#1

    2
    4
    bbcb
    aada
    aada
    3
    abc
    bbb
    bbb

    输出#1

    YES
    badc
    NO

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

首页