Cod sursa(job #2848448)

Utilizator anghelpatrickPatrick Anghel anghelpatrick Data 12 februarie 2022 16:33:36
Problema Ordine Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.85 kb
#include <fstream>

using namespace std;

ifstream cin("ordine.in");
ofstream cout("ordine.out");


int vf[30];
char v[1000001];
int main(void){

    char c;
    int n = 0;
    while(cin >> c){
        n++;
        vf[c-'a']++;
    }
    v[0] = '+';
    for(int i = 1;i<=n;i++){
        bool ok = 0;
        for(int j = 0;j<=26;j++){
            if(vf[j] > (n-i+1)/2 && 'a'+ j != v[i-1]){
                v[i] = (char)'a'+j;
                ok = 1;
                vf[j]--;
                break;
            }

        }
        if(!ok){
            for(int j = 0;j<=26;j++){
                if(vf[j] != 0 && 'a'+ j != v[i-1]){
                    v[i] = j+'a';
                    vf[j]--;
                    break;
                }
            }
        }
    }
    for(int i = 1;i<=n;i++){
        cout << v[i];
    }
}