169B - Replacing Digits - CodeForces Solution


greedy *1100

Please click on ads to support us..

Python Code:

n = list(input())
s = input()
f = [0]*10
for i in s:
    f[ord(i)-48] +=1
j = 9
i = 0
while(i<len(n) and j>=0):
    while j>=0 and f[j]==0:
        j -= 1 
    if ord(n[i])-48 < j:
        n[i] = j
        f[j]-=1
    i+=1
for i in n:
    print(i,end="")

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define vi vector<int> 
#define pii pair<int,int>
#define vii vector<pii> 
#define rep(i,a,b) for(int i=a; i<b; i++)
#define ff first
#define ss second
const ll M = 1e9+7;
const ll mm = 998244353;

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

string n,m;
cin >> n >> m;
 
vector<char> v;
for(auto x: m){
    v.push_back(x);
}

sort(v.begin(), v.end());

int temp = v.size()-1;
for(int i=0; i<n.length(); i++){
    if(n[i] < v[temp]) {
        n[i] = v[temp];
        temp--;
        if(temp < 0) break;
        }
    
    }

    cout << n << "\n";

return 0;
}


Comments

Submit
0 Comments
More Questions

868A - Bark to Unlock
873B - Balanced Substring
1401D - Maximum Distributed Tree
1716C - Robot in a Hallway
1688B - Patchouli's Magical Talisman
99A - Help Far Away Kingdom
622B - The Time
1688C - Manipulating History
1169D - Good Triple
1675B - Make It Increasing
588A - Duff and Meat
1541B - Pleasant Pairs
1626B - Minor Reduction
1680A - Minimums and Maximums
1713A - Traveling Salesman Problem
1713B - Optimal Reduction
1710A - Color the Picture
1686B - Odd Subarrays
251A - Points on Line
427C - Checkposts
1159A - A pile of stones
508A - Pasha and Pixels
912A - Tricky Alchemy
1249A - Yet Another Dividing into Teams
1713C - Build Permutation
1699A - The Third Three Number Problem
1617B - GCD Problem
841A - Generous Kefa
1690B - Array Decrements
1692C - Where's the Bishop