CF1394C.Boboniu and String
省选/NOI-
通过率:0%
时间限制:3.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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 p1,p2,…,p∣s∣ such that for all i (1≤i≤∣s∣), spi=ti.
Boboniu also defines 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 s1,s2,…,sn 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 maxi=1ndist(si,t).
Boboniu 将 BN-字符串定义为仅由字符 'B' 和 'N' 组成的字符串 s。
你可以在 BN-字符串 s 上执行以下操作:
- 删除 s 中的一个字符;
- 删除 s 中的一个子串
"BN"或"NB"; - 在 s 的末尾添加一个字符
'B'或'N'; - 在 s 的末尾添加一个字符串
"BN"或"NB"。
注意:若字符串 a 可通过从字符串 b 的开头删除若干(可能为零或全部)字符、并从结尾删除若干(可能为零或全部)字符而得到,则称 a 是 b 的一个子串。
Boboniu 认为两个 BN-字符串 s 和 t 相似,当且仅当满足以下条件:
- ∣s∣=∣t∣;
- 存在一个排列 p1,p2,…,p∣s∣,使得对所有 i(1≤i≤∣s∣),均有 spi=ti。
Boboniu 还将 s 与 t 之间的距离 dist(s,t) 定义为:使 s 变为与 t 相似的所需操作的最小次数。
现在 Boboniu 给出 n 个非空 BN-字符串 s1,s2,…,sn,要求你找出一个非空 BN-字符串 t,使得 t 到所有 si 的距离的最大值最小化,即最小化 maxi=1ndist(si,t)。
输入格式
The first line contains a single integer n (1≤n≤3⋅105).
Each of the next n lines contains a string si (1≤∣si∣≤5⋅105). It is guaranteed that si only contains 'B' and 'N'. The sum of ∣si∣ does not exceed 5⋅105.
第一行包含一个整数 n(1≤n≤3⋅105)。
接下来的 n 行中,每行包含一个字符串 si(1≤∣si∣≤5⋅105)。保证每个 si 仅由字符 'B' 和 'N' 组成。所有 ∣si∣ 的总和不超过 5⋅105。
输出格式
In the first line, print the minimum maxi=1ndist(si,t).
In the second line, print the suitable t.
If there are several possible t's, you can print any.
第一行输出最小的 maxi=1ndist(si,t)。
第二行输出一个满足条件的 t。
若存在多个可能的 t,输出任意一个即可。
输入输出样例
输入#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, dist(BN,BN)=0. So the maximum distance is 1.
在第一个例子中,dist(B,BN)=dist(N,BN)=1,dist(BN,BN)=0。因此最大距离为 1。
输入解题思路,AI测评打分。不知道怎么写?