Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 圆括号对的递归算法_Algorithm_Math_Recursion_Permutation_Proof - Fatal编程技术网

Algorithm 圆括号对的递归算法

Algorithm 圆括号对的递归算法,algorithm,math,recursion,permutation,proof,Algorithm,Math,Recursion,Permutation,Proof,我试图回答以下问题:“实现一个算法来打印n对括号的所有有效(即正确打开和关闭)组合。” 答案是:“我们的第一个想法可能是应用递归方法,通过在f(n-1)中添加几对括号来构建f(n)的解决方案。我们可以通过在每对现有括号中插入一对括号,以及在字符串开头插入一对括号来实现。” 我很难理解我们如何保证在每对现有的括号中插入一对括号,以及在字符串开头插入一对括号,将创建所有可能的解决方案。我们如何知道这不会产生重复的解决方案,或者遗漏一些正确的解决方案?有人能解释一下吗 (引文来源:破解编码采访)您描述

我试图回答以下问题:“实现一个算法来打印n对括号的所有有效(即正确打开和关闭)组合。”

答案是:“我们的第一个想法可能是应用递归方法,通过在f(n-1)中添加几对括号来构建f(n)的解决方案。我们可以通过在每对现有括号中插入一对括号,以及在字符串开头插入一对括号来实现。”

我很难理解我们如何保证在每对现有的括号中插入一对括号,以及在字符串开头插入一对括号,将创建所有可能的解决方案。我们如何知道这不会产生重复的解决方案,或者遗漏一些正确的解决方案?有人能解释一下吗


(引文来源:破解编码采访)

您描述的方法对f(1)和f(2)很有效。对于n>2,它不会遗漏任何内容,但会生成重复项

对于f(3),这是开始生成副本的时间。在f(2)的基础上,你有两个解“()”和“(())”。当您在该算法后面插入括号时,两个解决方案都会生成“()(())”。由于这个重复,最终得到了6个f(3)的解,而不是实际的5个

如果将该算法应用于f(5),它将生成f(1)到f(5)的33个总解。应该只有22个解决方案,因此您将获得10个副本

有一个非常常见的递归解决方案,涉及到多个括号的开始和结束计数。我见过的最好的解释是

下面是C语言中一种解决方案的示例:

// Source: http://www.geeksforgeeks.org/print-all-combinations-of-balanced-parentheses/
# include<stdio.h>
# define MAX_SIZE 100

void _printParenthesis(int pos, int n, int open, int close);

/* Wrapper over _printParenthesis()*/
void printParenthesis(int n)
{
  if(n > 0)
     _printParenthesis(0, n, 0, 0);
  return;
}     

void _printParenthesis(int pos, int n, int open, int close)
{
  static char str[MAX_SIZE];     

  if(close == n) 
  {
    printf("%s \n", str);
    return;
  }
  else
  {
    if(open > close) {
        str[pos] = '}';
        _printParenthesis(pos+1, n, open, close+1);
    }
    if(open < n) {
       str[pos] = '{';
       _printParenthesis(pos+1, n, open+1, close);
    }
  }
}

/* driver program to test above functions */
int main()
{
  int n = 4;
  printParenthesis(n);
  getchar();
  return 0;
}
//来源:http://www.geeksforgeeks.org/print-all-combinations-of-balanced-parentheses/
#包括
#定义最大尺寸100
void _print括号(int pos,int n,int open,int close);
/*_print括号()上的包装器*/
空打印括号(int n)
{
如果(n>0)
_打印括号(0,n,0,0);
返回;
}     
void _print括号(int pos,int n,int open,int close)
{
静态字符str[MAX_SIZE];
如果(关闭==n)
{
printf(“%s\n”,str);
返回;
}
其他的
{
如果(打开>关闭){
str[pos]='}';
_打印括号(位置+1,n,打开,关闭+1);
}
如果(打开
作为参考,我为您提供的算法制作了一个C版本:

// Initial funtion call
void f(int n)
{
    f(1, n, "");
}

// Recursive call
void f(int n, int max, string output)
{
    for (int i = 0; i < output.Length; i++)
    {
        if (output[i] == '(')
        {
            var inserted = output.Insert(i + 1, "()");
            Console.WriteLine(inserted);
            if (n < max)
                f(n + 1, max, inserted);
        }
    }

    // Pre-pend parens
    output = "()" + output;
    Console.WriteLine(output);
    if (n < max)
        f(n + 1, max, output);
}

f(4);
//初始函数调用
空f(整数n)
{
f(1,n,“”;
}
//递归调用
void f(int n,int max,字符串输出)
{
for(int i=0;i

链接:

如果答案对你有帮助,请随时投票,如果答案解决了你的问题,请接受。这有助于其他人了解哪些答案可能对你的问题有帮助。这也会提高你的声誉。