#include <bits/stdc++.h>
using namespace std;
using ll=int64_t;
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define _F(i,a,b,s) for(int i=(a);((s)>0?i<(b):i>(b));i+=(s))
#define _1(e) _F(i,0,e,1)
#define _2(i,e) _F(i,0,e,1)
#define _3(i,b,e) _F(i,b,e,(b)<(e)?1:-1)
#define _G(a,b,c,d,e,...) e
#define FOR(...) _G(__VA_ARGS__,_F,_3,_2,_1)(__VA_ARGS__)
int s[3], m[3];
string letter = "abc";
map<char, int> num = {{'a', 0}, {'b', 1}, {'c', 2}};
bool check(int x, int y) {
if (!s[x]) return 0;
s[x]--;
m[y]--;
bool ret = 1;
FOR (i, 3) {
int tot = 0;
FOR (j, 3) {
if (j != i) {
tot += s[j];
}
}
ret &= tot >= m[i];
}
s[x]++;
m[y]++;
return ret;
}
int main() {
ios::sync_with_stdio(0),cin.tie(0);
int n; cin >> n;
string a, b; cin >> a >> b;
FOR (i, n) {
s[num[a[i]]]++;
}
FOR (i, n) {
m[num[b[i]]]++;
}
vector<char> ans;
FOR (i, n) {
FOR (j, 3) {
if (letter[j] != b[i] && check(j, num[b[i]])) {
s[j]--;
m[num[b[i]]]--;
ans.pb(letter[j]);
break;
}
}
}
FOR (i, n) cout << ans[i];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
292 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 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 |