답안 #490340

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
490340 2021-11-27T08:45:44 Z niloyroot Igra (COCI17_igra) C++14
100 / 100
1 ms 460 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<ll>;
using pl = pair<ll,ll>;
#define pb push_back
#define form(m,it) for(auto it=m.begin(); it!=m.end(); it++)
#define forp(i,a,b) for(ll i=a; i<=b; i++)
#define forn(i,a,b) for(ll i=a; i>=b; i--)
#define newl '\n'
#define ff first
#define ss second
const ll mod = 1000000007;

void solve(){
    ll n; cin>>n;
    string a,b; cin>>a>>b;

    ll c[3]={0};
    forp(i,0,n-1){c[a[i]-'a']++;}

    ll cnt[3][n+1];
    memset(cnt,0,sizeof(cnt));
    forn(i,n-1,0){
        forp(j,0,2){
            cnt[j][i]=cnt[j][i+1]+(b[i]-'a'==j);
        }
    }
    string s="";
    bool g; ll sum;
    forp(i,0,n-1){
        forp(j,0,2){
            g=1;
            if(j==b[i]-'a'||c[j]<=0){continue;}
            forp(k,0,2){
                sum=0;
                forp(m,0,2){
                    if(m!=k){
                        sum+=c[m];
                        if(m==j){sum--;}
                    }
                }
                if(sum<cnt[k][i+1]){
                    g=0;
                }
            }
            if(g){
                c[j]--;
                s+=(j+'a'); break;
            }
        }
    }
    cout<<s<<newl;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t=1; //cin>>t;
    while(t--)solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 0 ms 460 KB Output is correct