Period of a String

시간 제한1초메모리 제한2048 MB

요약
각 문자열의 문자를 교환해 이전 문자열이 다음 문자열의 주기가 되도록 만들 수 있는지 판별하고, 가능하면 결과 문자열을 출력한다.
난이도

어려움10점 중 9점

유형
그리디, 문자열, 수학
정답자
아직 제출이 없습니다

문제

Randias has nn strings s_1,s_2,…,s_ns\_{1}, s\_{2}, \ldots, s\_{n}.

For two strings a=a_0a_1…a_p−1‾a = \overline{a\_{0} a\_{1} \ldots a\_{p-1}} and b=b_0b_1…b_q−1‾b = \overline{b\_{0} b\_{1} \ldots b\_{q-1}}, if for all ii (0≤i<q0 \le i < q), b_i=a_i mod pb\_{i} = a\_{i \bmod {p}}, we say that aa is a period of bb.

Now, Randias can perform the following operation:

  • Choose one string s_is\_{i} and choose two indices jj and kk (0≤j,k<∣s_i∣0 \le j, k < |s\_{i}|), then swap s_i,js\_{i,j} and s_i,ks\_{i,k}.

He can perform this operation any number of times. After all the operations, he wants the following to be true: for each 1<i≤n1 < i \le n, string s_i−1s\_{i-1} is a period of s_is\_{i}.

Help him to find the possible final strings, or determine it is impossible.

입력

Each test contains multiple test cases. The first line contains a single integer tt (1≤t≤1041 \le t \le 10^4) denoting the number of test cases. For each test case:

The first line contains a single integer nn (2≤n≤1052 \le n \le 10^5).

Then follow nn lines. The ii-th of these lines contains the string s_is\_{i} (1≤∣s_i∣≤5⋅1061 \le |s\_{i}| \le 5 \cdot 10^6). It is guaranteed that the strings only contain lowercase English letters.

It is guaranteed that the sum of nn does not exceed 10510^5, and the sum of ∣s_i∣|s\_{i}| does not exceed 5⋅1065 \cdot 10^6.

출력

For each test case, if it is possible to make s_i−1s\_{i-1} a period of s_is\_{i} for all ii after some operations, output "YES" (without quotes) on the first line. Then output nn strings in nn lines. The ii-th string s′_is'\_{i} represents the ii-th string after all operations. If there are multiple answers, output any one of them.

If it is impossible to do that, output "NO" (without quotes) on the first line.

예제1

  1. 예제 1

    입력
    4
    2
    abc
    abcd
    4
    bbcaa
    cabb
    acabbbb
    a
    3
    ab
    aab
    bbaaaaab
    3
    ab
    aab
    bbaaaaaa
    
    예상 출력
    NO
    YES
    abbca
    abbc
    abbcabb
    a
    YES
    ab
    aba
    abaabaab
    NO