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


int goodstr( char* s, int len ) {
    int pos, sublen;
    for ( sublen = 1; sublen <= len/2; sublen++ ) {
	for ( pos = 0; pos < len - sublen; pos++ ) {
	    int i;
	    for ( i = 0; i < sublen; i++ ) {
		if ( s[pos+i] != s[pos+i+sublen] ) {
		    goto no;
		}
	    }
	    return 0;
	    no:
	}
    }
    return 1;
}

char maxsymb;

int strsForSymbLen( int n, int len ) {
    char* s;
    int found = 0;
    int ipos = 0;

    s = (char*)malloc( len + 1 );
    for ( ipos = 0; ipos < len; ipos++ ) {
	s[ipos] = 'a';
    }
    s[len] = '\0';
    ipos = 0;
    while ( 1 ) {
	if ( goodstr( s, len ) ) {
	    printf("%s\n", s );
	    found++;
	    goto done;
	}
	ipos = 0;
	do {
	while ( s[ipos] == maxsymb ) {
	    s[ipos] = 'a';
	    ipos++;
	    if ( ipos == len ) {
		goto done;
	    }
	}
	s[ipos]++;
	} while ( (ipos + 1 < len) && (s[ipos] == s[ipos+1]) );
    }
    done:
    free( s );
    return found;
}

int maxdepth = 1;
char ds[4096];
int dfs( int deep ) {
    for ( ds[deep] = 'a'; ds[deep] <= maxsymb; ds[deep]++ ) if ( ds[deep-1] != ds[deep] ) {
	if ( goodstr( ds, deep + 1 ) ) {
	    if ( deep > maxdepth ) {
		ds[deep+1] = '\0';
		printf("%s\n",ds);
		maxdepth = deep;
		if ( deep+2 >= sizeof(ds) ) {
		    exit(0);
		}
	    }
	    dfs( deep+1 );
	}
    }
}

int main( int argc, char** argv ) {
    int n = 1, l, tot;
    if ( argc == 2 ) {
	printf("goodstr(%s) => %d\n", argv[1], goodstr( argv[1], strlen( argv[1] ) ) );
	return 0;
    }
#if 01
    ds[0] = 'a';
    maxsymb = 'a' + 3;
    dfs( 1 );
#else
//    while ( 1 ) {
    for ( n = 1; n < 3; ) {
	int found;
	l = 1;
	printf("n %d\n", n );
	tot = 0;
	maxsymb = 'a' + n;
	while ( (found = strsForSymbLen( n, l )) ) {
	    tot += found;
	    l++;
	    printf("l %d:\n", l );
	}
	printf("tot %d\n", tot );
	n++;
    }
#endif
}
