Cod sursa(job #290417)

Utilizator toni2007Pripoae Teodor Anton toni2007 Data 27 martie 2009 21:58:18
Problema Ordine Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.79 kb
#include <stdio.h>
#include <assert.h>
#include <string.h>

#define maxN 1000100

int C[maxN], N, A[27];
char s[maxN];

int main () {
	int i, j, maxim, pozmaxim, ok;

	freopen("ordine.in", "r", stdin);
	freopen("ordine.out", "w", stdout);

	gets(s); N = strlen(s);
	
	for (i = 0; i < N; ++ i)
		++ A[s[i] - 'a'];
	
	C[0] = 26;
	for (i = 1; i <= N; ++ i) {
		ok = true;
		for (j = 0; j < 26 && ok; ++ j)
			if (A[j] > (N - i + 1) / 2) {
				printf("%c", j + 'a');
				A[j] --;
				C[i] = j;
				ok = false;
			}
		if (! ok)	continue;
		ok = true; maxim = 0; pozmaxim = 26;
		for (j = 0; j < 26 && ok; ++ j)
			if (C[i - 1] != j && A[j] > maxim) {
				maxim = A[j];
				pozmaxim = j;
				ok = false;
			}
		assert(!!maxim && pozmaxim != 26);
		printf("%c", pozmaxim + 'a');
		A[pozmaxim] --;
		C[i] = pozmaxim;
	}
}