Submission #341840

#TimeUsernameProblemLanguageResultExecution timeMemory
341840alradIgra (COCI17_igra)C++17
100 / 100
2 ms620 KiB
#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; } int suma = dp[start][0]; int sumb = dp[start][1]; int sumc = dp[start][2]; return (x[0] + x[1] - sumc >= 0 && x[1] + x[2] - suma >= 0 && x[0] + x[2] - sumb >= 0); }; 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; }
#Verdict Execution timeMemoryGrader output
Fetching results...