Cod sursa(job #1727320)

Utilizator alittlezzCazaciuc Valentin alittlezz Data 10 iulie 2016 15:34:04
Problema Ordine Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.2 kb
#include <fstream>
#include <iostream>
#include <math.h>
#include <algorithm>

using namespace std;
#define ll long long
#define llu long long unsigned
#define pb push_back
#define mp make_pair

string problemName = "ordine";
string inFile = problemName+".in";
string outFile = problemName+".out";
ifstream fin(inFile.c_str());
ofstream fout(outFile.c_str());

int ap[26];

int main(){
    string s;
    fin>>s;
    int i,st,dr;
    int n = s.size();
    for(i = 0;i < n;i++){
        ap[s[i]-'a']++;
    }
    string ans = "";
    char ch1,ch2;
    for(st = 0;st < 26;st++){
        if(ap[st]){
            ch1 = st+'a';
            for(dr = st+1;dr < 26 && ap[st];dr++){
                if(ap[dr]){
                    ch2 = dr+'a';
                    for(i = 1;i < ap[st] && ap[dr];i++){
                        ans += ch1;
                        ans += ch2;
                        ap[st]--;
                        ap[dr]--;
                    }
                }
                if(ap[st] == 1){
                    ap[st]--;
                    ans += ch1;
                    break;
                }
            }
        }
    }
    fout<<ans;
    return 0;
}