Cod sursa(job #112356)

Utilizator gabitzish1Gabriel Bitis gabitzish1 Data 4 decembrie 2007 20:29:04
Problema Ordine Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.66 kb
#include <stdio.h>
#include <string.h>
int a[30], n;


int main()
{
	freopen("ordine.in","r",stdin);
	freopen("ordine.out","w",stdout);
	char x;
	scanf("%c",&x);
	do
	{
		a[x - 'a' + 1]++;
		n++;
	scanf("%c",&x);
	} while (x != '\n');
	int i, last=40;
	i = 0;
	while (n>0)
	{ 
		for (i = 1; i <= 26; i++)
		    if (a[i] > n / 2)
			{
				x = i + 'a' - 1;
				printf("%c",x);
				n--;
				a[i]--;
				last = i;
			}
		i=1;
		while (!a[i] && i < 27) i++;
		if (i<=26)
		{
		if (i == last)
		i++;
		while (!a[i]) i++;
		x = i + 'a' - 1;
		printf("%c",x);
		a[i]--;
		n--;
		last = i;
		}
	}
	printf("\n");
	return 0;
}