题解:CF1137B Camp Schedule
DerRichter · · 题解
题意
给定两个 01 字符串
思路
首先,一定是拿
代码
思路虽简单,实现上还是有些细节的。详见代码。
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAXN = 5e5 + 10;
string s, t;
int fail[MAXN], cnt[MAXN][2];
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> s >> t;
int n = s.size(), m = t.size();
s = '#' + s; t = '#' + t;
pair<int, int> res = {0, 0};
cnt[1][t[1] - '0'] = 1;
for (int i = 2, j = 0; i <= m; i++) {
for (; j && t[i] != t[j + 1]; j = fail[j]);
j += t[i] == t[j + 1];
fail[i] = j;
for (int _ : {0, 1}) {
cnt[i][_] = cnt[i - 1][_] + (t[i] - '0' == _);
}
}
for (int i = 1; i <= n; i++) {
res.first += s[i] == '0';
res.second += s[i] == '1';
}
string k = t.substr(1);
if (res.first < cnt[m][0] || res.second < cnt[m][1] || m > n) {
cout << s.substr(1);
return 0;
}
res.first -= cnt[m][0], res.second -= cnt[m][1];
while (1) {
if (res.first < cnt[m][0] - cnt[fail[m]][0] || res.second < cnt[m][1] - cnt[fail[m]][1]) {
break;
}
res.first -= cnt[m][0] - cnt[fail[m]][0];
res.second -= cnt[m][1] - cnt[fail[m]][1];
k += t.substr(fail[m] + 1);
}
for (int i = 1; i <= res.first; i++) k += '0';
for (int i = 1; i <= res.second; i++) k += '1';
cout << k;
return 0;
}