Submission #310855

# Submission time Handle Problem Language Result Execution time Memory
310855 2020-10-08T08:26:25 Z kaplanbar Igra (COCI17_igra) C++14
100 / 100
1 ms 384 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int c[3], dp[5005][3];
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    string a, b;
    cin >> a >> b;
    for(int i = 0; i < n; i++) {
        c[a[i] - 'a']++;
    }
    for(int i = n - 1; i >= 0; i--) {
        dp[i][b[i] - 'a']++;
        if(i != n - 1) {
            for(int j = 0; j < 3; j++) dp[i][j] += dp[i + 1][j];
        }
    }
    auto check = [&](int idx) -> bool {
        if(idx == n - 1) return true;
        int s = 0;
        for(int i = 0; i < 3; i++) {
            s += c[i];
        }
        for(int i = 0; i < 3; i++) {
            if(s - c[i] < dp[idx + 1][i]) return false;
        }
        return true;
    };
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < 3; j++) {
            if(c[j] && j != b[i] - 'a') {
                c[j]--;
                if(check(i)) {
                    cout << (char)(j + 'a');
                    break;
                }
                c[j]++;
            }
        }
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct