Submission #477389

#TimeUsernameProblemLanguageResultExecution timeMemory
477389largefenceIgra (COCI17_igra)C++17
100 / 100
1 ms332 KiB
#include <bits/stdc++.h>
using namespace std;

int cnt[3], mp[5243][4];
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    //freopen("ws.INP", "r", stdin);
    //freopen("ws.OUT", "w", stdout);

    memset(cnt, 0, sizeof cnt);
    memset(mp, 0, sizeof mp);
    int n; cin >> n;
    string a, b; cin >> a >> b;
    for (int i = n - 1; i >= 0; i--) {
        cnt[a[i] - 'a']++;
        for (int j = 0; j < 3; j++)
            mp[i][j] = mp[i + 1][j] + ((b[i] - 'a') == j);
    } string res = "";
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < 3; j++) {
            if (!cnt[j] || (b[i] - 'a') == j) continue;
            bool chk = true;
            for (int c = 0; c < 3 && chk; c++) {
                int tmp = 0;
                for (int d = 0; d < 3; d++)
                    if (c != d) tmp += cnt[d] - (d == j);
                if (mp[i + 1][c] > tmp) chk = false;
            }
            if (chk) {
                res += (j + 'a');
                cnt[j]--;
                break;
            }
        }
    } cout << res;
    return 0;
}
/* In case of emergency,
   ___ _____   _____   _   _ ___
  / __|_ _\ \ / / __| | | | | _ \
 | (_ || | \ V /| _|  | |_| |  _/
  \___|___| \_/ |___|  \___/|_|

*/
#Verdict Execution timeMemoryGrader output
Fetching results...