All pastes #526122 Raw Edit

Untitled

public text v1 · immutable
#526122 ·published 2007-05-31 17:55 UTC
rendered paste body
void recurse(int n, int m, char* stack, filled = 0)
{
	// filled denotes how many elements of our stack are filled [ with valid data ]
	if (filled == 0)
	{
		// initiallize our base cases, 0 though (n - m)
		//  - no point in setting anything higher than n-m because it will be pruned
		for (int x = 0; x < n - m; ++x)
		{
			stack[0] = 'a' + x;	// 'b' = 'a' + 1, 'c' = 'a' + 2, etc.
			recurse(n, m, stack, filled + 1);
		}
		return;
	}
	if (filled == m)
	{
		// if our stack is full, display it
		display(stack, m);
		return
	}
	// stack[filled - 1] was the last char we put in.
	//  extract that number (as element of n) by subtracting 'a'
	// starting at the next element, iterate up as far to n as will still allow us to fill
	// any unfilled elements of the stack (m - filled), thus, n - (m - filled)
	for (int x = (int)(stack[filled - 1] - 'a') + 1; x < n - (m - filled); ++x)
	{
		stack[filled] = 'a' + x;
		recurse(n, m, stack, filled + 1);
	}
	return;
}

stack = new char[3];
recurse(5, 3, stack)