46. Sohu (operation ):
How many matching arrangement methods can be applied to the four pairs of parentheses? For example, two pairs of parentheses can have two types: () and (())
It is similar to 12 low-level questions. If 0 is used to represent "(" and 1 is used to represent ")", the minimum 8 bits of digits are 4 1 and 4 0, and from low to high to 1 and 0, the number of occurrences of 0 cannot exceed 1.
/* 46. Sohu (operation): How many matching arrangement methods can be used for the four pairs of parentheses? For example, two pairs of parentheses can be: () and () */# include <stdio. h> int c_bits (int n) {int result = 0; For (; n; N & = n-1, result ++); return result;} int main () {int I; int ways = 0; // 0 indicates "(" 1 indicates ")" 0 must be before 1 for (I = 0; I <(1 <7); I ++) {If (c_bits (I) = 4) {int one_n = 0; int zero_n = 0; for (Int J = 0; j <8; j ++) {If (I> J) & 1) = 0) {zero_n + = 1 ;} else {one_n + = 1;} If (zero_n> one_n) {break;} If (one_n = 4 & zero_n = 4) {ways ++; printf ("way % d:", ways); For (Int J = 7; j> = 0; j --) {If (I> J) & 1) = 0) {printf ("(") ;}else {printf (")") ;}} printf ("\ n") ;}} return 0 ;}
There is a way on the Internet that is similar to mine. The difference lies in determining whether the requirements are met:
Http://www.cnblogs.com/GoAhead/archive/2012/05/30/2525824.html
Loop all the binary numbers within 8 bits. For each bit of each binary number, the values from high to low are added in sequence. If the value is 0, the values are-1, and the values are 1 and 1, the result of each addition must be greater than or equal to 0.
The result of adding all bits should be 0. A combination is used to satisfy the two conditions.
The disadvantage of my method is that you need to change the number after the number of parentheses changes.
The tree method is also mentioned:
We can solve this problem by generating a binary tree and redefine a data structure. The data structure is as follows:
Struct node {
Int data; // 0 or 1
Int num0; // the number of times that 0 appears.
Int num1; // the number of times that 1 appears.
Struct node * lchild;
Struct node * rchild;
};
At the same time, we need to use a queue to save the pointer of the leaf node, in order to reduce the time complexity
The procedure is as follows:
(1) Use element 1 to generate the root node. At the same time, num1 ++, num0 = 0, lchild = NULL, rchld = NULL;
(2) retrieve the first element of the queue from the queue and compare the num1 and num0 values. When num1 = 4, the node no longer increases. If num1 is greater than num0, generate left and right children for this node. Left child data = 1, num0 = parent node num0; num1 = parent node num1 + 1; right child data = 0, num0 = parent node num0 + 1, num1 = parent node num1;
How to num1 = num0, add only one left child 1, and add new nodes to the queue
(3) Repeat Step (2) know that the queue is empty
(4) counting the number of leaf nodes is the result
I didn't take it into consideration because I didn't give the code.
Http://blog.csdn.net/lihappy999/article/details/7395943 has a common method to achieve full search with recursion, then the determination is verified through
#include<iostream>#include<cassert>#include <vector>using namespace std ;void Print(vector<char> v){ for (vector<char>::iterator beg=v.begin();beg!=v.end();++beg) cout<<*beg<<" "; cout<<endl;}void MatchNums(int nSize,int nLen,vector<char> &v){ int nLeftBrackets=0; int nRightBrackets=0; for (vector<char>::iterator beg=v.begin();beg!=v.end();++beg) { if(*beg==‘(‘) nLeftBrackets++; else nRightBrackets++; if(nRightBrackets>nLeftBrackets) return; if(nLeftBrackets+nRightBrackets==nSize&&nLeftBrackets==nRightBrackets) Print(v); } if (nLen>0) { v.push_back(‘(‘); MatchNums(nSize,nLen-1,v); v.pop_back(); v.push_back(‘)‘); MatchNums(nSize,nLen-1,v); v.pop_back(); }}int main(){ vector <char> v; int n=6; MatchNums(n,n,v); return 1;}