Cod sursa(job #485570)

Utilizator Teodor94Teodor Plop Teodor94 Data 18 septembrie 2010 20:42:21
Problema Ordine Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.67 kb
#include<cstdio>

const int N=1000002;

char s[N],ss[N];
int nr,fr[N];

void citire()
{
	gets(s);
	for (nr=0;s[nr];++nr)
		fr[(int)s[nr]-(int)'a']++;
}

void rez()
{
	nr--;
	for (int j=0;j<26;++j)
		if (fr[j])
		{
			ss[0]=(char)97+j;
			fr[j]--;
			break;
		}
	int j,k;
	char x;
	for (int i=1;i<=nr;++i)
	{
		j=0;
		while (fr[j]==0)
			j++;
		x=(char)97+j;
		if (x!=ss[i-1])
		{
			ss[i]=x;
			fr[j]--;
		}
		else
		{
			k=j+1;
			while (fr[k]==0)
				++k;
			ss[i]=(char)97+k;
			fr[k]--;
		}
	}
	puts(ss);
}

int main()
{
	freopen("ordine.in","r",stdin);
	freopen("ordine.out","w",stdout);
	citire();
	rez();
	return 0;
}