Click here to Skip to main content
15,400,390 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 5e5 + 14, z = 26;

int main(){
    ios::sync_with_stdio(0), cin.tie(0);
    int t;
    cin >> t;
    while(t--){
        int n, k;
        string s;
        cin >> n >> k >> s;
        int value[z], per[z], need[z] = {};
        vector<int> have[z];
        for(int i = 0; i < n; i++)
            have[s[i] - 'a'].push_back(i);
        for(int i = 0; i < z; i++)
            cin >> value[i];
        iota(per, per + z, 0);
        sort(per, per + z, [&](int i, int j){  return value[i] != value[j] ? value[i] > value[j] : i < j;  });
        set<int> picked;
        for_each(per, per + z, [&](int i){
            for(int j = 0; j < have[i].size(); j++)
                if(have[i].size() - j <= k || k && s[*picked.lower_bound(have[i][j])] - 'a' > i)
                    picked.insert(have[i][j]), k--;
        });
        for(auto i : picked)
            cout << s[i];
        cout << '\n';
    }
}


What I have tried:

can someone convert??

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 5e5 + 14, z = 26;

int main(){
    ios::sync_with_stdio(0), cin.tie(0);
    int t;
    cin >> t;
    while(t--){
        int n, k;
        string s;
        cin >> n >> k >> s;
        int value[z], per[z], need[z] = {};
        vector<int> have[z];
        for(int i = 0; i < n; i++)
            have[s[i] - 'a'].push_back(i);
        for(int i = 0; i < z; i++)
            cin >> value[i];
        iota(per, per + z, 0);
        sort(per, per + z, [&](int i, int j){  return value[i] != value[j] ? value[i] > value[j] : i < j;  });
        set<int> picked;
        for_each(per, per + z, [&](int i){
            for(int j = 0; j < have[i].size(); j++)
                if(have[i].size() - j <= k || k && s[*picked.lower_bound(have[i][j])] - 'a' > i)
                    picked.insert(have[i][j]), k--;
        });
        for(auto i : picked)
            cout << s[i];
        cout << '\n';
    }
}
Posted
Updated 10-May-21 20:04pm
Comments
Richard MacCutchan 10-May-21 10:15am
   
Learn C++, learn Python, convert the code.
Rick York 11-May-21 2:06am
   
No, it is impossible. One must rewrite it.

Quote:
can someone convert??


This is not a code conversion service: we are not here to translate code for you.
Even if we did, what you would end up with would not be "good code" in the target language – they are based on very different frameworks, and what makes something work in one language does not always "translate" directly into another.
So what you end up with is very poor code, that is difficult if not impossible to maintain, that can’t be upgraded nicely, and that will cause you immense headaches if the original is changed. And it’ll be a nightmare to debug if it doesn’t work "straight out of the box".
Instead, use the source code as a specification for a new app written in and for the target language / framework and write it from scratch using the original as a "template". You will get a much, much better result that will save you a lot of time in the long run.
   
It is a good exercise for you, since it uses some features of the C++ standard library.
I suggest you to examine the C++ code, clean it (for instance removing the unused typedef long long ll;) and then start converting.
You could start with simple tasks, like converting the iota[^] function.
If you have any doubt on specific tasks, then ask here.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900