Submission #736225

# Submission time Handle Problem Language Result Execution time Memory
736225 2023-05-05T10:40:48 Z onebit1024 Igra (COCI17_igra) C++17
100 / 100
2 ms 340 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define pb push_back
#define all(c) c.begin(), c.end()
#define endl "\n"

const double PI=3.141592653589;


void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define dbg(x...) cerr << "LINE(" << __LINE__ << ") -> " <<"[" << #x << "] = ["; _print(x)
#else
#define dbg(x...)
#endif


int n;
string s,t;
vector<int>cnts(4),cntt(4);
bool place(char x){
    // place char x in the resultant array
    vector<int>cnt = cnts;
    for(int j = 0;j<=cnts[1];++j){
        // brute force over the number of b's that you take
        for(int k = 0;k<3;++k)cnt[k] = cnts[k];
        cnt[1]-=j;
        int left = cntt[0]-j;
        if(left < 0)break;
        cnt[2]-=left;
        if(cnt[2] < 0)continue;
        left = cntt[1]-cnt[2];
        cnt[2] = 0;
        if(left>0)cnt[0]-=left;
        if(cnt[0]<0)continue;
        if(cnt[0]+cnt[1] >= cntt[2])return 1;
    }
    return 0;
}
void solve()
{
    cin >> n >> s >> t;
    string res;
    for(int i = 0;i<n;++i)cntt[t[i]-'a']++,cnts[s[i]-'a']++;
    for(int i = 0;i<n;++i){
        cntt[t[i]-'a']--;
        for(char x = 'a';x<='c';++x){
            if(cnts[x-'a'] && x!=t[i]){
                // try placing character x
                // you can only place it if it doesn't prevent you from filling up the remaining suffix
                cnts[x-'a']--;
                if(place(x)){
                    res+=x;
                    break;
                }
                cnts[x-'a']++;  
            }
        }
    }
    cout << res << endl;
}   

int32_t main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);


    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    

    int T=1;
    for(int i = 1;i<=T;++i)
    {
        // cout << "Case #" << i << ": ";
        solve();
    }
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 324 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 324 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 2 ms 340 KB Output is correct
10 Correct 2 ms 340 KB Output is correct