#AT1962. F - String Cards
F - String Cards
F - String Cards
Score : $500$ points
Problem Statement
We have $N$ cards. The $i$-th card has a string $S_i$ written on it.
Find the lexicographically smallest string that can be obtained by choosing $K$ of these cards and concatenating them in any order.
Below, let $S_i$ denote the $i$-th character of $S$. Also, if $S$ is lexicographically smaller than $T$, we will denote that fact as $S \lt T$; if $S$ is lexicographically larger than $T$, we will denote that fact as $S \gt T$.
- Let $L$ be the smaller of the lengths of $S$ and $T$. For each $i=1,2,\dots,L$, we check whether $S_i$ and $T_i$ are the same.
- If there is an $i$ such that $S_i \neq T_i$, let $j$ be the smallest such $i$. Then, we compare $S_j$ and $T_j$. If $S_j$ comes earlier than $T_j$ in alphabetical order, we determine that $S \lt T$ and quit; if $S_j$ comes later than $T_j$, we determine that $S \gt T$ and quit.
- If there is no $i$ such that $S_i \neq T_i$, we compare the lengths of $S$ and $T$. If $S$ is shorter than $T$, we determine that $S \lt T$ and quit; if $S$ is longer than $T$, we determine that $S \gt T$ and quit.
Constraints
- $1 \leq K \leq N \leq 50$
- $1 \leq |S_i| \leq 50$
- $S_i$ consists of lowercase English letters.
Input
Input is given from Standard Input in the following format:
Output
Print the answer.
4 3
ode
zaaa
r
atc
atcoder
Note that it is not possible to reverse or permute the string written on a card.
For example, ode
written on the first card cannot be used as edo
or deo
.
5 2
z
z
zzz
z
zzzzzz
zz
There may be a pair $i, j$ $(i\neq j)$ such that $S_i = S_j$.