Header Ad

HackerEarth Arjit and Printing Press problem solution

In this HackerEarth Arjit and Printing Press problem solution Arjit has his own printing press, Bainik Dhaskar (BD). He feels that words on their own simply aren't beautiful enough. So, he wishes to make a Super Manuscript (SM) machine. Now what does this machine do?
The SM machine aims to make words as beautiful as they can be by making a word as lexicographically small as possible. Arjit, being the resourceful person he is, has a reserve string from which we can choose characters that will replace characters in the original word that BD's SM machine wishes to transform.
Keep in mind that once you have used a letter in the reserve string, it is removed from the reserve.
As Arjit is busy with other work at BD, it's your work to take care of programming SM :)
Note that you cannot modify the original order of the letters in the word that has to be transformed. You can only replace its letters with those in the reserve.


HackerEarth Arjit and Printing Press problem solution


HackerEarth Arjit and Printing Press problem solution.

#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e4 + 5;
char W[maxn], R[maxn];

int main()
{
int t;
scanf("%d", &t);
while (t--)
{
scanf("%s%s", W, R);
vector<int> cnt(26);
for (int i = 0; R[i]; i++)
cnt[R[i] - 'a']++;
for (int i = 0; W[i]; i++)
for (int j = 0; j < 26; j++)
if (cnt[j] > 0)
if (j + 'a' < W[i])
{
W[i] = j + 'a';
cnt[j]--;
break;
}
printf("%s\n", W);
}
return 0;
}

Second solution

#include<iostream>
#include <map>
#include <vector>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <functional>
#include <vector>
#include <stack>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <assert.h>
using namespace std;
int main()
{
int t;
cin>>t;
assert(t>=1 && t<=10);
while (t--)
{
string w,r;
cin>>w>>r;
assert(w.size()>=1 && w.size()<=10000);
assert(r.size()>=1 && r.size()<=10000);
sort(r.begin(),r.end());
int i,j=0;
for(i=0;i<w.size()&&j<r.size();i++)
{
if(w[i]>r[j])
{
w[i]=r[j];
j++;
}
}
cout<<w<<endl;
}
return 0;
}

Post a Comment

0 Comments