Photolog

Through the Looking-Glass
2010-10-12: Through the Looking-Glass
My radio speaks is binary!
2010-10-10: My radio speaks is binary!
Gigaminx: (present for my birthday)
2010-09-16: Gigaminx: (present for my birthday)
Trini on bike
2010-09-05: Trini on bike
Valporquero
2010-08-28: Valporquero
My new bike!
2010-08-22: My new bike!
Mario and Ana's wedding
2010-08-13: Mario and Ana's wedding
Canyoning in Guara
2010-08-07: Canyoning in Guara
Trini and Mari in Marbella
2010-08-05: Trini and Mari in Marbella
Trini and Chelo in Tabarca
2010-08-03: Trini and Chelo in Tabarca
Valid XHTML 1.1
Log in
Back to list of problems

Krypton Factor

129.c

#include <stdio.h>
#include <string.h>

int len;
char seq[10000];

int
hard(void) {
	int i,j;

#if DEBUG
	printf("hard(%*.*s)", len, len, seq);
#endif
	for (i=1; i<=len/2; i++) {
		for (j=0; j<=len-2*i; j++) {
			if (!strncmp(seq+j, seq+j+i, i)) {
#if DEBUG
				printf(" = 0\n");
#endif
				return 0;
			}
		}
	}
#if DEBUG
	printf(" = 1\n");
#endif
	return 1;
}

void
new(int L) {
	int i;

	len++;
	for (i=0; i<L; i++) {
		seq[len-1] = 'A'+i;
		if (hard()) {
			return;
		}
	}

	while (1) {
		len--;
		while (seq[len-1] < 'A'+L-1) {
			seq[len-1]++;
			if (hard()) {
				return;
			}
		}
	}
}

int
main(void) {
	int n,L,i;

	while (1) {
		scanf("%d %d", &n, &L);
		if (n==0 && L==0) {
			return 0;
		}
		len=0;
		for (i=0; i<n; i++) {
			new(L);
		}
		for (i=0; i<len; i++) {
			if (i) {
				if (i % 64 == 0) {
					printf("\n");
				} else if (i % 4 == 0) {
					printf(" ");
				}
			}
			printf("%c", seq[i]);
		}
		printf("\n%d\n", len);
	}
}