#P16455. [UOI 2026] Unique Letters

    ID: 16441 Type: RemoteJudge 300ms 512MiB Tried: 0 Accepted: 0 Difficulty: 6 Uploaded By: Tags>Special Judge2026UOI(乌克兰)

[UOI 2026] Unique Letters

题目描述

You are given two strings ss and tt, consisting of lowercase Latin letters.

You may arbitrarily permute the characters in string ss and arbitrarily permute the characters in string tt.

We define the uniquization\textit{uniquization} of a string xx as the following process. First, an auxiliary string aa is empty. Then the characters of string xx are processed from left to right. Suppose the current character is cc.

  • If character cc is not present in the current string aa, then character cc is appended to the end of string aa.
  • If character cc is already present in the current string aa, then the suffix of string aa up to and including the first occurrence of character cc is removed from aa. In this case, the currently processed character cc is not added.

The string aa obtained after processing all characters will be called the result of uniquization of this string.

Determine whether it is possible to obtain strings ss' and tt' such that:

  • ss' is a permutation of the characters of string ss;
  • tt' is a permutation of the characters of string tt;
  • the result of uniquization of string ss' is equal to tt'.

输入格式

The first line contains string ss (1s2105)(1 \le |s| \le 2 \cdot 10^5).

The second line contains string tt (1t2105)(1 \le |t| \le 2 \cdot 10^5).

Both strings consist only of lowercase Latin letters.

输出格式

If it is impossible to obtain the required strings ss' and tt', output NO\tt{NO}.

Otherwise, output YES\tt{YES}. Then output two strings ss' and tt' such that:

  • ss' is a permutation of the characters of string ss;
  • tt' is a permutation of the characters of string tt;
  • the result of uniquization of string ss' is equal to tt'.
bbcdfe
fe
YES
bcdbef
ef
bbfcbbd
f
YES
bcdbfbb
f
cgbfedc
gfbc
NO

提示

For the first sample:

We permute string ss as bcdbef\texttt{bcdbef}, and string tt as ef\texttt{ef}.

Construct string aa:

  • after character b\texttt{b}, we have a=ba = \texttt{b};
  • after character c\texttt{c}, we have a=bca = \texttt{bc};
  • after character d\texttt{d}, we have a=bcda = \texttt{bcd};
  • after character b\texttt{b}, the letter b\texttt{b} is already present in string aa, so we remove the suffix up to and including the first occurrence of b\texttt{b}. After that, aa becomes an empty string;
  • after character e\texttt{e}, we have a=ea = \texttt{e};
  • after character f\texttt{f}, we have a=efa = \texttt{ef}.

At the fourth step, letters c\texttt{c} and d\texttt{d} are also removed from string aa, not only letter b\texttt{b}.

The resulting string aa is equal to the permuted string tt, so an answer exists.

For the second sample:

We permute string ss as bcdbfbb\texttt{bcdbfbb}, and leave string tt as f\texttt{f}.

Construct string aa:

  • after character b\texttt{b}, we have a=ba = \texttt{b};
  • after character c\texttt{c}, we have a=bca = \texttt{bc};
  • after character d\texttt{d}, we have a=bcda = \texttt{bcd};
  • after character b\texttt{b}, the letter b\texttt{b} is already present in string aa, so we remove the suffix up to and including the first occurrence of b\texttt{b}. After that, aa becomes an empty string;
  • after character f\texttt{f}, we have a=fa = \texttt{f};
  • after character b\texttt{b}, we have a=fba = \texttt{fb};
  • after character b\texttt{b}, the letter b\texttt{b} is already present in string aa, so we remove the suffix up to and including the first occurrence of b\texttt{b}. After that, a=fa = \texttt{f}.

At the fourth step, letters c\texttt{c} and d\texttt{d} are also removed from string aa, not only letter b\texttt{b}.

The resulting string aa is equal to the permuted string tt, so an answer exists.

In the third sample, it can be proved that for no permutation of string cgbfedc\texttt{cgbfedc} will the result of uniquization be a permutation of string gfbc\texttt{gfbc}.

Scoring

  • (44 points): s=t=1|s|=|t|=1;
  • (88 points): ss can be obtained from tt by permuting letters;
  • (88 points): all letters in ss are distinct;
  • (1212 points): only one letter in string ss appears more than once;
  • (2424 points): s=aaaaabbbbbcccccddddds=\texttt{aaaaabbbbbcccccddddd};
  • (1616 points): s8|s| \le 8;
  • (2828 points): no additional constraints.