Cod sursa(job #1258228)

Utilizator rzvrzvNicolescu Razvan rzvrzv Data 8 noiembrie 2014 17:04:14
Problema Ordine Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.67 kb
#include<cstdio>
#include<cstring>

using namespace std;

int a[26],n,i,b,c,j;
char s[1000002];

int main()
{
    freopen("ordine.in","r",stdin);
    freopen("ordine.out","w",stdout);
    gets(s+1);
    n=strlen(s+1);
    for(i=1;i<=n;i++)
    {
        a[s[i]-'a']++;
    }
    c=100;
    b=100;
    for(i=1;i<=n;i++)
    {
        j=0;
        while(a[j]==0||j==c)
        {
            j++;
        }
        b=c;
        c=j;
        for(j=c;j<26;j++)
        {
            if(a[j]==(n-i+1)/2+1&&j!=b)
            {
                c=j;
            }
        }
        a[c]--;
        printf("%c",'a'+c);
    }
    printf("\n");
    return 0;
}