constructive algorithms greedy implementation strings

Please click on ads to support us..

C++ Code:

#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <vector>
#include <stack>
using namespace std;


void solve() {
    string s; cin >> s;
    sort(s.begin(), s.end());
    vector<char> sol (s.size(), ' ');
    map<char, int> mp;
    for (int i = 0; i < s.size(); i++) {
        mp[s[i]]++;
    }
    int left = 0;
    int right = s.size()-1;
    for (auto & [k,v] : mp) {
        if (v % 2 == 0) {
            int steps = v / 2; // fill this many chars
            while (steps--) {
                sol[left] = k;
                sol[right] = k;
                left += 1;
                right -= 1;
            }
            mp[k] = 0;
        } else { // odd number, need to quit here
            if (v > 2) {
                int steps = (v-1) / 2;
                while (steps--) {
                    sol[left] = k;
                    sol[right] = k;
                    left += 1;
                    right -= 1;
                }
            }
            mp[k] = 1;
            break;
        }
    }
    // check how many are remaining
    vector<char> keys; 
    for (auto & [k,v] : mp) {
        if (v > 0) keys.push_back(k);
    }
    // break into cases by the number of remaining letters
    if (keys.size() == 0) {
        for (char c : sol)  {cout << c;}
        cout << endl;
    } 
    else if (keys.size() == 1){
        string r;
        int times = mp[keys[0]];
        while (times--) {
            r += keys[0];
        }
        // print result
        for (int i = 0; i < left; i++) {
            cout << sol[i];
        }
        cout << r;
        for (int i = right + 1; i < s.size(); i++){
            cout << sol[i];
        }
        cout << endl;
    } 
    else if (keys.size() == 2) { 
        // abbb case
        string r;
        int times = mp[keys[1]] + 1;
        while (times--) {
            r += keys[1];
        }
        r[(r.size())/2] = keys[0];
        // print result
        for (int i = 0; i < left; i++) {
            cout << sol[i];
        }
        cout << r;
        for (int i = right + 1; i < s.size(); i++){
            cout << sol[i];
        }
        cout << endl;
    } 
    else {
        // multiple 
        string r; 
        for (int i = 1; i < keys.size(); i++) {
            int reps = mp[keys[i]];
            while (reps--) {
                r += keys[i];
            }
        }
        r += keys[0];
        // print result
        for (int i = 0; i < left; i++) {
            cout << sol[i];
        }
        cout << r;
        for (int i = right + 1; i < s.size(); i++){
            cout << sol[i];
        }
        cout << endl;
    }








}



int main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int tt = 1;
    cin >> tt;
    while (tt--) {
        solve();
    }

    return 0;
}


Comments

Submit
0 Comments
More Questions

1025D - Recovering BST
439A - Devu the Singer and Churu the Joker
1323A - Even Subset Sum Problem
1095A - Repeating Cipher
630F - Selection of Personnel
630K - Indivisibility
20B - Equation
600B - Queries about less or equal elements
1015A - Points in Segments
1593B - Make it Divisible by 25
680C - Bear and Prime 100
1300A - Non-zero
1475E - Advertising Agency
1345B - Card Constructions
1077B - Disturbed People
653A - Bear and Three Balls
794A - Bank Robbery
157A - Game Outcome
3B - Lorry
1392A - Omkar and Password
489A - SwapSort
932A - Palindromic Supersequence
433A - Kitahara Haruki's Gift
672A - Summer Camp
1277A - Happy Birthday Polycarp
577A - Multiplication Table
817C - Really Big Numbers
1355A - Sequence with Digits
977B - Two-gram
993A - Two Squares