eolymp
bolt
Спробуйте наш новий інтерфейс для відправки розв'язків
Задачі

The Swapping Game

The Swapping Game

Last year you invented a game which can be played using a board and a die (singular of dice), this year you invented another new game which can be played using a single string of some letters. The game starts with a string of \textbf{N} lower case English letters ('\textbf{a}' to '\textbf{z}'), and you can only swap two different characters in this string, and you can make this step zero or more times. Your goal is to reach the lexicographically smallest string after doing zero or more moves. But there are some constraints on the final string. For each position, it must be a letter of some given letters (the given letters are not necessary the same for each position). For example, the first letter must be '\textbf{a}' or '\textbf{b}', the second letter must be '\textbf{b}' or '\textbf{c}', and so on. Note that these constraints are on the final string only, which means you can make moves which cause invalid strings to reach a valid string after some more moves. Given the initial string and the constraints on each position, your task is to write a program to find the lexicographically smallest valid string after making zero or more moves. \textit{\textbf{Note:}} \textit{When comparing two different strings of the same length, the lexicographically smaller one is the one with a smaller letter on the first place where they differ.} \InputFile Your program will be tested on one or more test cases. The first line of the input will be a single integer \textbf{T}, the number of test cases (\textbf{1} ≤ \textbf{T} ≤ \textbf{100}). Followed by the test cases, each test case starts with a line containing the initial string \textbf{S} which consists of \textbf{N} lower case English letters (\textbf{1} ≤ \textbf{N} ≤ \textbf{100}). Followed by \textbf{N} lines each line contains a string \textbf{C_\{i \}}which consists of \textbf{L_i} distinct lower case English letters (\textbf{1} ≤ \textbf{L_i} ≤ \textbf{5}) which are the valid letters for the \textbf{i}th position in the final string. Each letter in each \textbf{C_i} will appear at least once in \textbf{S}. \OutputFile For each test case, print on a single line the lexicographically smallest valid string you can get after zero or more moves. If there is no such valid string print "\textbf{NO SOLUTION}" (without the quotes).
Ліміт часу 1 секунда
Ліміт використання пам'яті 64 MiB
Вхідні дані #1
2
abcde
abcde
a
abcde
abcde
abcde
abcde
ab
ab
ab
abcde
abcde
Вихідні дані #1
bacde
NO SOLUTION
Джерело Arab Collegiate Programming Contest 2012