Cod sursa(job #1605845)

Utilizator CraiuAndrei Craiu Craiu Data 19 februarie 2016 15:47:23
Problema Ordine Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.98 kb
#include <bits/stdc++.h>

using namespace std;

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

char s[1000005], v[1000005];
int f[30];
int n;

int main()
{
    int i, j;
    bool ok;
    fin >> (s + 1);
    n = strlen(s + 1);
    for(i = 1; s[i]; i++)
    {
        j = s[i] - 'a' + 1;
        f[j]++;
    }
    for(i = 1; i <= n; i++)
    {
        ok = false;
        for(j = 1; j <= 26; j++)
            if(f[j] >= (n - i + 1) / 2 + 1 && j + 'a' - 1 != v[i - 1])
            {
                ok = true;
                v[i] = j + 'a' - 1;
                f[j]--;
                break;
            }
        if(!ok)
        {
            for(j = 1; j <= 26; j++)
                if(f[j] != 0 && j + 'a' - 1 != v[i - 1])
                {
                    v[i] = j + 'a' - 1;
                    f[j]--;
                    break;
                }
        }
    }
    //for(i = 1; i <= n; i++)
        fout << (v + 1);
    return 0;
}