답안 #963287

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
963287 2024-04-14T20:17:55 Z shezitt Igra (COCI17_igra) C++14
100 / 100
2 ms 348 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
#include <cstring>
#include <queue>
#include <numeric>

using namespace std;

using ll = long long;

#define int ll
#define fore(i, a, b) for(int i=a; i<b; ++i)
#define endl '\n'
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define pb push_back
#define ii pair<int,int>
#define vi vector<int>

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int n;
    cin >> n;

    string s, m, ans;

    cin >> s >> m;

    int scnt[3] = {};
    int mcnt[3] = {};
    int stmp[3] = {};

    for(char c : s){
        scnt[c - 'a']++;
    }

    for(char c : m){
        mcnt[c - 'a']++;
    }

    auto check = [&](int i, int p) -> bool {

        fore(j, 0, 3){
            stmp[j] = scnt[j];    
        }

        if(stmp[p] == 0) return 0;

        if(m[i] == 'a'+p) {
            return 0;
        }

        stmp[p]--;

        int ba, bc, ac, ab, ca, cb;
        fore(bba, 0, stmp[1]+1){

            ba = bba;
            bc = stmp[1] - ba;
            ac = mcnt[2] - bc;
            ca = mcnt[0] - ba;
            ab = stmp[0] - ac;
            cb = stmp[2] - ca;

            bool ok = min({ba, bc, ac, ca, ab, cb}) >= 0;

            ok &= ac + ab == stmp[0] && ba + bc == stmp[1] && ca + cb == stmp[2];

            if(ok){
                return 1;
            }

        }

        return 0;

    };

    fore(i, 0, n){
        mcnt[m[i] - 'a']--;
        fore(j, 0, 3){
            if(check(i, j)){
                scnt[j]--;
                ans.pb('a'+j);
                break;
            }
        }
    }
    
    cout << ans;

}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 2 ms 344 KB Output is correct
10 Correct 2 ms 348 KB Output is correct