
#include <stdio.h>
#include <strings.h>

#define NO	0
#define YES	!NO
#define TABS	"            "
#define MAXLEN	256

#define STRING(len)	((char *) malloc(len + 1))

#define BADARGS	1

int colors, maxlen;
int gotten = 0;

main(argc,argv)
int argc;
char *argv[];
{
void makenodes();

if (argc < 2)
  {
  fprintf(stderr,"Usage: %s maxlen [colors]\n",argv[0]);
  exit(BADARGS);
  }
else if (argc > 2)
  colors = atoi(argv[2]);
else
  colors = 2;

maxlen = atoi(argv[1]);

makenodes(maxlen - 1);
}

/***********************************************************************/
void
makenodes(maxdepth)
int maxdepth;
{
int apchk(), depth = 0;
char curlet, *curstring = STRING(maxdepth);

curstring[0] = '\0';
curlet = 'A';

while (depth >= 0)
  {
  while (curlet <= 'A' - 1 + colors)
    {
#ifdef DIAG
    printf("%s makenoding with string %s%c, depth %d.\n",
	TABS+12-depth,curstring,curlet,depth);
#endif
    if (apchk(curstring,curlet))
      curlet++;
    else
      if (depth < maxdepth)
        {
        curstring[depth] = curlet;
        curstring[depth+1] = '\0';
        depth += 1;
        curlet = 'A';
        }
      else
	{
        printf("%s%c\n",curstring,curlet);
	curlet++;
	}
    }
  depth -= 1;
  curlet = curstring[depth] + 1;
  curstring[depth] = '\0';
  }
}

/***********************************************************************/
int
apchk(qq,q)
char *qq ,q;
{
int lqq, f, s, t;

t = lqq = strlen(qq);
if (lqq < 2) return NO;

for (f=lqq % 2; f <= lqq - 2; f += 2)
  {
  s = (f + t) / 2;
  if ((qq[f] == qq[s]) && (qq[s] == q))
    {
#ifdef DIAG
    printf("%s%c is rejected for %c in positions %d,%d,%d.\n",
	qq,q,q,f,s,t);
#endif
    return YES;
    }
  }
return NO;
}
