Cod sursa(job #1506434)

Utilizator eu3neuomManghiuc Teodor-Florin eu3neuom Data 20 octombrie 2015 17:50:44
Problema Ordine Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.76 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("ordine.in");
ofstream fout("ordine.out");

const int Sigma = 30;

int v[Sigma];

int main(){
    string S;
    fin >> S;
    for(int i = 0; i < S.size(); i++){
        v[S[i] - 'a']++;
    }
    int last = -1, pos = 1;
    int n = S.size();
    for(int i = 0; i < n; i++){
        for(int j = 0; j < Sigma; j++){
            if(v[j] && j != last){
                pos = j;
                break;
            }
        }
        for(int j = 0; j < Sigma; j++){
            if(j != last && v[j] > (n - i) / 2){
                pos = j;
                break;
            }
        }
        v[pos]--;
        fout << (char)('a' + pos);
        last = pos;
    }
    return 0;
}