程序员面试金典: 9.9 递归和动态规划 9.6打印n对括号的全部有效组合

#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <sstream>
using namespace std;

/*
问题:实现一种算法,打印n对括号的全部有效组合(即左右括号正确配对)
分析:这是卡特兰数。印象中好像是
      总解法个数 =(1/n) * (2n)! / ( (n-1)! * (n+1)! ) 
	  比如n=2,
	  把左括号认为为1,右括号认为0,它必须满足从任意数字n开始,前面n-1个数中1的个数>=0的个数

	  用递归来做,确定第一个为1,后续每次检查0和1哪个符合,需要用哈希计数。
	  2n*2=4n
	  时间复杂度为O(n)

输出:
2(2对括号)
输出:
(()),()()
*/

const int MAXSIZE = 10000;

bool isOk(int pos , char* result , int value , int n)
{
	if(pos < 0 || value < 0 || value > 1)
	{
		return false;
	}
	int hashArr[2];
	memset(hashArr , 0 , sizeof(hashArr));
	for(int i = 0 ; i < pos; i++)
	{
		int ch = result[i] - '0';
		if(ch > 1 || ch < 0)
		{
			continue;
		}
		hashArr[ch]++;
	}
	hashArr[value]++;
	int count0 = hashArr[0];
	int count1 = hashArr[1];
	//易漏,注意coun1的个数最多只能为n
	if(count1 >= count0 && count1 <= n)
	{
		return true;
	}
	else
	{
		return false;
	}
}

void getCombination(int pos , char* result , vector< string >& results , int n)
{
	if(pos < 0 || n < 1 || NULL == result)
	{
		return;
	}
	stringstream ss;
	//总长度为2n
	if( pos == 2*n )
	{
		result[pos] = '\0';
		string sResult(result);
		results.push_back(sResult);
	}
	else
	{
		//摆放第pos个位置为1或0,必须满足当前摆放后,字符串中1的个数>=0的个数
		for(int i = 0 ; i <= 1; i++)
		{
			if( isOk(pos , result , i , n) )
			{
				
				//如果可以就递归处理
				result[pos] = (char)(i + '0');
				getCombination(pos + 1 , result , results , n);
			}
		}
	}
}

string turnToBracket(string& str)
{
	if(str.empty())
	{
		return str;
	}
	int size = str.size();
	char ch;
	stringstream ss;
	for(int i = 0 ; i < size ; i++)
	{
		ch = str.at(i);
		if( '1' == ch )
		{
			ss << "(";
		}
		else if('0' == ch)
		{
			ss << ")";
		}
	}
	return ss.str();
}

void print(vector<string>& results)
{
	if(results.empty())
	{
		cout << "No Result!" << endl;
		return;
	}
	int count = 0;
	for(vector<string>::iterator it = results.begin() ; it != results.end() ; it++)
	{
		if(count)
		{
			cout << "," << turnToBracket(*it);
		}
		else
		{
			cout << turnToBracket(*it);
		}
		count++;
	}
	cout << endl;
}

void process()
{
	int n;
	vector< string >  results;
	int pos;
	char result[MAXSIZE];
	while(cin >> n)
	{
		pos = 0;
		results.clear();
		getCombination(pos , result , results , n);
		print(results);
	}
}

int main(int argc, char* argv[])
{
	process();
	getchar();
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值