답안 #341840

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
341840 2020-12-31T07:13:00 Z alrad Igra (COCI17_igra) C++17
100 / 100
2 ms 620 KB
#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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 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 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 2 ms 620 KB Output is correct
10 Correct 1 ms 620 KB Output is correct