Cod sursa(job #485606)

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

const int N=1000005;

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

void citire()
{
	gets(s);
	for (int i=0;s[i];++i)
		if (s[i]>='a' && s[i]<='z')
		{
			nr=i;
			fr[s[i]-'a'+1]++;
		}
}

void rez()
{
	int last=-1,j,k;
	for (int j=1;j<=26;++j)
		if (fr[j])
		{
			fr[j]--;
			last=j;
			printf("%c",'a'+last-1);
			break;
		}
	for (int i=1;i<=nr;++i)
	{
		j=1;
		while (fr[j]==0)
			j++;
		if (j!=last)
		{
			last=j;
			fr[j]--;
			printf("%c",'a'+last-1);
		}
		else
		{
			k=j+1;
			while (fr[k]==0)
				++k;
			last=k;
			fr[k]--;
			printf("%c",'a'+last-1);
		}
	}
}

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