Cod sursa(job #127223)

Utilizator slayer4uVictor Popescu slayer4u Data 23 ianuarie 2008 16:52:12
Problema Ordine Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.03 kb
#include <stdio.h>
#include <string.h>

long found, i, j, n, count[30], rez[10001];
char s[10001];

int main()
{
	freopen ("ordine.in", "rt", stdin);
	freopen ("ordine.out", "wt", stdout);

	fgets(s, 10001, stdin);

	n = strlen(s) - 1;

	for (i = 0; i <= n; i ++)
		count[s[i] - 'a']++;

	found = 0;
	for (j = 0; j <= 25; j ++)
		if (count[j] == (n + 1) / 2 + 1)
		{
			rez[i] = j;
			count[j] --;
			found = 1;
			break;
		}
	if (!found)
		for (j = 0; j <= 25; j ++)
			if (count[j] && j != rez[i - 1])
			{
				rez[i] = j;
				count[j] --;
				break;
			}

	for (i = 1; i <= n; i ++)
	{
		found = 0;
		for (j = 0; j <= 25; j ++)
			if (count[j] == ((n + 1) - (i + 1)) / 2 + 1 && j != rez[i - 1])
			{
				rez[i] = j;
				count[j] --;
				found = 1;
				break;
			}
		if (!found)
			for (j = 0; j <= 25; j ++)
				if (count[j] && j != rez[i - 1])
				{
					rez[i] = j;
					count[j] --;
					break;
				}
	}

	for (i = 0; i <= n; i ++)
		printf("%c", (char) rez[i] + 'a');

	return 0;
}