CF1394C.Boboniu and String

省选/NOI-

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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

You can perform the following operations on the BN-string ss:

  • Remove a character of ss.
  • Remove a substring "BN" or "NB" of ss.
  • Add a character 'B' or 'N' to the end of ss.
  • Add a string "BN" or "NB" to the end of ss.

Note that a string aa is a substring of a string bb if aa can be obtained from bb 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 ss and tt are similar if and only if:

  • ∣s∣=∣t∣|s|=|t|.
  • There exists a permutation p1,p2,…,p∣s∣p_1, p_2, \ldots, p_{|s|} such that for all ii (1≤i≤∣s∣1\le i\le |s|), spi=tis_{p_i}=t_i.

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

Now Boboniu gives you nn non-empty BN-strings s1,s2,…,sns_1,s_2,\ldots, s_n and asks you to find a non-empty BN-string tt such that the maximum distance to string ss is minimized, i.e. you need to minimize max⁡i=1ndist(si,t)\max_{i=1}^n \text{dist}(s_i,t).

Boboniu 将 BN-字符串定义为仅由字符 'B' 和 'N' 组成的字符串 ss。

你可以在 BN-字符串 ss 上执行以下操作:

  • 删除 ss 中的一个字符;
  • 删除 ss 中的一个子串 "BN" 或 "NB";
  • 在 ss 的末尾添加一个字符 'B' 或 'N';
  • 在 ss 的末尾添加一个字符串 "BN" 或 "NB"。

注意:若字符串 aa 可通过从字符串 bb 的开头删除若干(可能为零或全部)字符、并从结尾删除若干(可能为零或全部)字符而得到,则称 aa 是 bb 的一个子串。

Boboniu 认为两个 BN-字符串 ss 和 tt 相似,当且仅当满足以下条件:

  • ∣s∣=∣t∣|s|=|t|;
  • 存在一个排列 p1,p2,…,p∣s∣p_1, p_2, \ldots, p_{|s|},使得对所有 ii(1≤i≤∣s∣1\le i\le |s|),均有 spi=tis_{p_i}=t_i。

Boboniu 还将 ss 与 tt 之间的距离 dist(s,t)\text{dist}(s,t) 定义为:使 ss 变为与 tt 相似的所需操作的最小次数。

现在 Boboniu 给出 nn 个非空 BN-字符串 s1,s2,…,sns_1,s_2,\ldots, s_n,要求你找出一个非空 BN-字符串 tt,使得 tt 到所有 sis_i 的距离的最大值最小化,即最小化 max⁡i=1ndist(si,t)\max_{i=1}^n \text{dist}(s_i,t)。

输入格式

The first line contains a single integer nn (1≤n≤3⋅1051\le n\le 3\cdot 10^5).

Each of the next nn lines contains a string sis_i (1≤∣si∣≤5⋅1051\le |s_i| \le 5\cdot 10^5). It is guaranteed that sis_i only contains 'B' and 'N'. The sum of ∣si∣|s_i| does not exceed 5⋅1055\cdot 10^5.

第一行包含一个整数 nn(1≤n≤3⋅1051\le n\le 3\cdot 10^5)。

接下来的 nn 行中,每行包含一个字符串 sis_i(1≤∣si∣≤5⋅1051\le |s_i| \le 5\cdot 10^5)。保证每个 sis_i 仅由字符 'B' 和 'N' 组成。所有 ∣si∣|s_i| 的总和不超过 5⋅1055\cdot 10^5。

输出格式

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

In the second line, print the suitable tt.

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

第一行输出最小的 max⁡i=1ndist(si,t)\max_{i=1}^n \text{dist}(s_i,t)。

第二行输出一个满足条件的 tt。

若存在多个可能的 tt,输出任意一个即可。

输入输出样例

  • 输入#1

    3
    B
    N
    BN

    输出#1

    1
    BN
  • 输入#2

    10
    N
    BBBBBB
    BNNNBBNBB
    NNNNBNBNNBNNNBBN
    NBNBN
    NNNNNN
    BNBNBNBBBBNNNNBBBBNNBBNBNBBNBBBBBBBB
    NNNNBN
    NBBBBBBBB
    NNNNNN

    输出#2

    12
    BBBBBBBBBBBBNNNNNNNNNNNN
  • 输入#3

    8
    NNN
    NNN
    BBNNBBBN
    NNNBNN
    B
    NNN
    NNNNBNN
    NNNNNNNNNNNNNNNBNNNNNNNBNB

    输出#3

    12
    BBBBNNNNNNNNNNNN
  • 输入#4

    3
    BNNNBNNNNBNBBNNNBBNNNNBBBBNNBBBBBBNBBBBBNBBBNNBBBNBNBBBN
    BBBNBBBBNNNNNBBNBBBNNNBB
    BBBBBBBBBBBBBBNBBBBBNBBBBBNBBBBNB

    输出#4

    12
    BBBBBBBBBBBBBBBBBBBBBBBBBBNNNNNNNNNNNN

说明/提示

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

在第一个例子中,dist(B,BN)=dist(N,BN)=1\text{dist(B,BN)}=\text{dist(N,BN)}=1,dist(BN,BN)=0\text{dist(BN,BN)}=0。因此最大距离为 11。

输入解题思路,AI测评打分。不知道怎么写?

首页