Codeforces Round 664 (Div. 1)
C
Boboniu and String
Codeforces
2600
原题
← BD →
翻译中…

Boboniu defines BN-string as a string $s$ of characters 'B' and 'N'.

翻译中…

You can perform the following operations on the BN-string $s$:

翻译中…
  • Remove a character of $s$.
  • Remove a substring "BN" or "NB" of $s$.
  • Add a character 'B' or 'N' to the end of $s$.
  • Add a string "BN" or "NB" to the end of $s$.
翻译中…

Note that a string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.

翻译中…

Boboniu thinks that BN-strings $s$ and $t$ are similar if and only if:

翻译中…
  • $|s|=|t|$.
  • There exists a permutation $p_1, p_2, \ldots, p_{|s|}$ such that for all $i$ ($1\le i\le |s|$), $s_{p_i}=t_i$.
翻译中…

Boboniu also defines $\text{dist}(s,t)$, the distance between $s$ and $t$, as the minimum number of operations that makes $s$ similar to $t$.

翻译中…

Now Boboniu gives you $n$ non-empty BN-strings $s_1,s_2,\ldots, s_n$ and asks you to find a non-empty BN-string $t$ such that the maximum distance to string $s$ is minimized, i.e. you need to minimize $\max_{i=1}^n \text{dist}(s_i,t)$.

翻译中…

Input

翻译中…

The first line contains a single integer $n$ ($1\le n\le 3\cdot 10^5$).

翻译中…

Each of the next $n$ lines contains a string $s_i$ ($1\le |s_i| \le 5\cdot 10^5$). It is guaranteed that $s_i$ only contains 'B' and 'N'. The sum of $|s_i|$ does not exceed $5\cdot 10^5$.

翻译中…

Output

翻译中…

In the first line, print the minimum $\max_{i=1}^n \text{dist}(s_i,t)$.

翻译中…

In the second line, print the suitable $t$.

翻译中…

If there are several possible $t$'s, you can print any.

翻译中…

Note

翻译中…

In the first example $\text{dist(B,BN)}=\text{dist(N,BN)}=1$, $\text{dist(BN,BN)}=0$. So the maximum distance is $1$.

翻译中…

样例

Input #1
3
B
N
BN
Output #1
1
BN
InputCopy OutputCopy
Input #2
10
N
BBBBBB
BNNNBBNBB
NNNNBNBNNBNNNBBN
NBNBN
NNNNNN
BNBNBNBBBBNNNNBBBBNNBBNBNBBNBBBBBBBB
NNNNBN
NBBBBBBBB
NNNNNN
Output #2
12
BBBBBBBBBBBBNNNNNNNNNNNN
InputCopy OutputCopy
Input #3
8
NNN
NNN
BBNNBBBN
NNNBNN
B
NNN
NNNNBNN
NNNNNNNNNNNNNNNBNNNNNNNBNB
Output #3
12
BBBBNNNNNNNNNNNN
InputCopy OutputCopy
Input #4
3
BNNNBNNNNBNBBNNNBBNNNNBBBBNNBBBBBBNBBBBBNBBBNNBBBNBNBBBN
BBBNBBBBNNNNNBBNBBBNNNBB
BBBBBBBBBBBBBBNBBBBBNBBBBBNBBBBNB
Output #4
12
BBBBBBBBBBBBBBBBBBBBBBBBBBNNNNNNNNNNNN
InputCopy OutputCopy
题目信息
题号
1394C
时间限制
3 s
内存限制
256 MB
标签
二分
计算几何
三分

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。