#include <bits/stdc++.h>
using namespace std;
using ld = long double;
/*
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("-O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
*/
template <class T> inline T gcd(T a , T b) { return !a ? b : gcd(b % a , a); }
template <class T> inline T lcm(T a , T b) {return (a * b) / gcd(a , b) ; }
mt19937 rnd(time(0));
#define all(x) x.begin(), x.end()
#define debug(x) { cerr << #x << " = " << x << endl; }
void solve() {
int n;
cin >> n;
string a, b;
cin >> a >> b;
vector<int> let(3, 0);
for (char c : a) {
let[int(c - 'a')]++;
}
vector<vector<int>> dp(n, vector<int>(3, 0));
for (int i = n - 1; i >= 0; i--) {
dp[i][int(b[i] - 'a')]++;
if (i + 1 < n) {
for (int t = 0; t < 3; t++) {
dp[i][t] += dp[i + 1][t];
}
}
}
auto check = [&](int start, vector<int> x) {
if (start == n) {
return true;
}
bool sumc = (x[0] + x[1] - dp[start][2] >= 0);
bool suma = (x[1] + x[2] - dp[start][0] >= 0);
bool sumb = (x[0] + x[2] - dp[start][1] >= 0);
return (suma && sumb && sumc);
};
string ans = "";
for (int i = 0; i < n; i++) {
for (int t = 0; t < 3; t++) {
if (t != int(b[i] - 'a') && let[t] > 0) {
let[t]--;
if (check(i + 1, let)) {
ans += char(t + 'a');
break;
}
let[t]++;
}
}
}
cout << ans << '\n';
return;
}
signed main() {
ios_base :: sync_with_stdio(0);
cin.tie(0) , cout.tie(0);
int t = 1;
while (t-- > 0) {
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
620 KB |
Output is correct |
10 |
Correct |
1 ms |
620 KB |
Output is correct |