Cod sursa(job #2451822)

Utilizator MateiAruxandeiMateiStefan MateiAruxandei Data 28 august 2019 12:40:23
Problema Ordine Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.66 kb
#include <fstream>
#include <cstring>

using namespace std;

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

int fr[30], which;
char sir[1000005];

int getMax()
{
    int maxx = 0;
    for(int i = 0; i <= 26; ++i)
    {
        if(fr[i] > maxx)
        {
            maxx = fr[i];
            which = i;
        }
    }
    return maxx;
}

int main()
{
    fin >> sir;

    int lg = strlen(sir);
    for(int i = 0; i < lg; ++i)
        fr[sir[i] - 'a']++;

    int mx = 0, remain = lg, last = -1;
    for(int i = 0; i < lg; ++i)
    {
        mx = getMax();
        if(2 * mx > remain)
        {
            while(i < lg)
            {
                if(last == which)
                {
                    for(int j = 0; j <= 26; ++j)
                        if(fr[j] != 0 && j != which)
                        {
                            fout << (char)(j + 'a');
                            fr[j]--;
                            last = j;
                            break;
                        }
                }
                else
                {
                    fout << (char)(which + 'a');
                    last = which;
                }
                ++i;
            }
        }
        else
        {
            for(int j = 0; j <= 26; ++j)
                if(fr[j] != 0)
                {
                    if(last != j)
                    {
                        fout << (char)(j + 'a');
                        fr[j]--;
                        last = j;
                        break;
                    }
                }
        }
        --remain;
    }
    return 0;
}