Cod sursa(job #2582195)

Utilizator ililogIlinca ililog Data 16 martie 2020 14:47:07
Problema Ordine Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.97 kb
using namespace std;
#include<bits/stdc++.h>

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

struct Key 
{ 
    int freq; 
    char ch; 
    
    bool operator<(const Key &k) const
    { 
        return ch > k.ch; 
    } 
}; 

string str;
int n;
int fr[27];
priority_queue< Key > pq;

int main() {
    
    fin >> str;
    n = str.size();
    
    for (int i = 0; i<str.size(); i++) {
        fr[str[i] - 'a']++;
    }
    
    for (char i = 'a'; i<='z'; i++) {
        if (fr[i - 'a'] > 0) {
            pq.push(Key {fr[i - 'a'], i});
        }
    }

    str = "";
    
    Key prev = {-1, '#'};
    
    while (!pq.empty()) {
        
        Key k = pq.top();
        pq.pop();
        
        str += k.ch;
        
        if (prev.freq > 0) {
            pq.push(prev);
        }
        
        k.freq--;
        prev = k;
    }
    
    fout << str;
    
    fin.close();
    fout.close();

    return 0;
}