Cod sursa(job #485572)

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

const int N=1000002;

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

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

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

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