Cod sursa(job #1607027)

Utilizator dnprxDan Pracsiu dnprx Data 20 februarie 2016 19:35:48
Problema Ordine Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.97 kb
#include <bits/stdc++.h>
using namespace std;

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

int main()
{
    int i, j;
    bool ok;
    ifstream fin("ordine.in");
    ofstream fout("ordine.out");
    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;
                }
        }
    }
    fout << (v + 1) << "\n";
    fout.close();
    return 0;
}