Cod sursa(job #1934024)

Utilizator 1475369147896537415369Andrei Udriste 1475369147896537415369 Data 21 martie 2017 04:13:35
Problema Ordine Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.83 kb
#include<cstdio>
#include<cctype>
using namespace std;

int frequence[26], N, index, y;
char ch, prev;

int main(){

    freopen("ordine.in", "r", stdin);
    freopen("ordine.out", "w", stdout);

    while(isalpha(ch = getchar())){
        frequence[ch - 'a']++;
        N++;
    }

    while(N){

        y = 1;

        for(int i = 0; i < 26; i++){

            if(frequence[i] && i + 'a' != prev && y){
                index = i;
                ch = i + 'a';
                y = 0;
            }
            if(frequence[i] >= N / 2 + 1){
                index = i;
                ch = i + 'a';
                break;
            }
        }

        if(ch != prev){
            putchar(ch);
            N--;
            prev = ch;
            frequence[index]--;
        }
    }
    return 0;
}