目录

CF1394C-Boboniu and String

CF1394C-Boboniu and String

题目:

题目描述:

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) $ .

输入格式:

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 $ .

输出格式:

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.

样例:

样例输入 1:

1
2
3
4
3
B
N
BN

样例输出 1:

1
2
1
BN

样例输入 2:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
10
N
BBBBBB
BNNNBBNBB
NNNNBNBNNBNNNBBN
NBNBN
NNNNNN
BNBNBNBBBBNNNNBBBBNNBBNBNBBNBBBBBBBB
NNNNBN
NBBBBBBBB
NNNNNN

样例输出 2:

1
2
12
BBBBBBBBBBBBNNNNNNNNNNNN

样例输入 3:

1
2
3
4
5
6
7
8
9
8
NNN
NNN
BBNNBBBN
NNNBNN
B
NNN
NNNNBNN
NNNNNNNNNNNNNNNBNNNNNNNBNB

样例输出 3:

1
2
12
BBBBNNNNNNNNNNNN

样例输入 4:

1
2
3
4
3
BNNNBNNNNBNBBNNNBBNNNNBBBBNNBBBBBBNBBBBBNBBBNNBBBNBNBBBN
BBBNBBBBNNNNNBBNBBBNNNBB
BBBBBBBBBBBBBBNBBBBBNBBBBBNBBBBNB

样例输出 4:

1
2
12
BBBBBBBBBBBBBBBBBBBBBBBBBBNNNNNNNNNNNN

思路:

实现:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <bits/stdc++.h>
using namespace std;
#ifndef use_ios11
#define use_ios11
using namespace std;
struct ins
{
    int ans;
    ins() { ans = 1; }
#define endl '\n'
    void read()
    {
    }
    void read1(char &s)
    {
        char c = getchar();
        for (; !isprint(c) || c == ' ' || c == '\n' || c == '\t'; c = getchar())
            ;
        s = c;
        if (c == EOF)
            ans = 0;
    }
    void read1(string &s)
    {
        s = "";
        char c = getchar();
        for (; !isprint(c) || c == ' ' || c == '\n' || c == '\t'; c = getchar())
            ;
        for (; isprint(c) && c != ' ' && c != '\n' && c != '\t'; c = getchar())
            s += c;
        if (c == EOF)
            ans = 0;
    }
    template <typename T>
    void read1(T &n)
    {
        T x = 0;
        int f = 1;
        char c = getchar();
        for (; !isdigit(c); c = getchar())
        {
            if (c == '-')
                f = -1;
            if (c == EOF)
            {
                ans = 0;
                return;
            }
        }
        for (; isdigit(c); c = getchar())
            x = x * 10 + c - 48;
        n = x * f;
        if (c == EOF)
            ans = 0;
        if (c != '.')
            return;
        T l = 0.1;
        while ((c = getchar()) <= '9' && c >= '0')
            x = x + (c & 15) * l, l *= 0.1;
        n = x * f;
        if (c == EOF)
            ans = 0;
    }
    void write() {}
    void write1(string s)
    {
        int n = s.size();
        for (int i = 0; i < n; i++)
            putchar(s[i]);
    }
    void write1(const char *s)
    {
        int n = strlen(s);
        for (int i = 0; i < n; i++)
            putchar(s[i]);
    }
    void write1(char s) { putchar(s); }
    void write1(float s, int x = 6)
    {
        char y[10001];
        sprintf(y, "%%.%df", x);
        printf(y, s);
    }
    void write1(double s, int x = 6)
    {
        char y[10001];
        sprintf(y, "%%.%dlf", x);
        printf(y, s);
    }
    void write1(long double s, int x = 6)
    {
        char y[10001];
        sprintf(y, "%%.%dLf", x);
        printf(y, s);
    }
    template <typename T>
    void write1(T n)
    {
        if (n < 0)
            n = -n, putchar('-');
        if (n > 9)
            write1(n / 10);
        putchar('0' + n % 10);
    }
    template <typename T>
    friend ins operator>>(ins x, T &n);
    template <typename T>
    friend ins operator<<(ins x, T n);
    operator bool() { return ans; }
};
template <typename T>
ins operator>>(ins x, T &n)
{
    if (!x.ans)
        return x;
    x.read1(n);
    return x;
}
template <typename T>
ins operator<<(ins x, T n)
{
    x.write1(n);
    return x;
}
ins yin;
ins yout;
#endif
int n;
const int maxn = 4e5;
string s[maxn];
int ss[maxn], b[maxn], bb[maxn];
void pre()
{
    for (int i = 1; i <= n; i++)
    {
        int k = 1e6;
        for (int j = 0; j < s[i].size(); j++)
        {
            if (s[i][j] == 'B')
                ++b[i];
            else
                ++ss[i];
        }
    }
}
pair<int, int> ans;
int check(int x)
{
    ans = make_pair(0, 0);
    int mix = INT_MAX, miy = INT_MAX, maxx = INT_MIN, may = INT_MIN, miz = INT_MAX, maz = INT_MIN;
    for (int i = 1; i <= n; i++)
    {
        mix = min(b[i] + x, mix);
        maxx = max(b[i] - x, maxx);
        miy = min(ss[i] + x, miy);
        may = max(ss[i] - x, may);
        miz = min(b[i] - ss[i] + x, miz);
        maz = max(b[i] - ss[i] - x, maz);
    }
    maxx = max(maxx, 0);
    may = max(may, 0);
    if (mix < maxx)
        return 0;
    if (miy < may)
        return 0;
    if (miz < maz)
        return 0;
    if (maxx - miy > miz || mix - may < maz)
        return 0;
    ans = make_pair(min(mix, miy + miz), min(min(mix, miy + miz) - maz, miy));
    return 1;
}
int main()
{
    yin >> n;
    for (int i = 1; i <= n; i++)
    {
        yin >> s[i];
    }
    pre();
    long long l = 0, r = 1e6;
    while (l < r)
    {
        int mid = l + r >> 1;
        if (check(mid))
            r = mid;
        else
            l = mid + 1;
    }
    yout << l << endl;
    check(l);
    for (int i = 1; i <= ans.first; i++)
        yout << 'B';
    for (int i = 1; i <= ans.second; i++)
        yout << 'N';
    return 0;
}