📜  递归打印可以从单词列表列表中形成的所有句子

📅  最后修改于: 2022-05-13 01:57:08.384000             🧑  作者: Mango

递归打印可以从单词列表列表中形成的所有句子

给定一个单词列表列表如何通过递归一次从列表中提取一个单词来打印所有可能的句子?
例子:

Input: {{"you", "we"},
        {"have", "are"},
        {"sleep", "eat", "drink"}}

Output:
  you have sleep
  you have eat
  you have drink
  you are sleep
  you are eat
  you are drink
  we have sleep
  we have eat
  we have drink
  we are sleep
  we are eat
  we are drink 

我们强烈建议您最小化您的浏览器并首先自己尝试。
这个想法是基于一个简单的深度优先遍历。我们从第一个列表的每个单词作为输出句子的第一个单词开始,然后对剩余的列表进行递归。
下面是上述想法的 C++ 实现。在下面的实现中,列表的输入列表被认为是一个二维数组。如果我们仔细观察,我们可以观察到代码非常接近graph的DFS。

C++
// C++ program to print all possible sentences from a list of word list
#include 
#include 
 
#define R 3
#define C 3
using namespace std;
 
// A recursive function to print all possible sentences that can be formed
// from a list of word list
void printUtil(string arr[R][C], int m, int n, string output[R])
{
    // Add current word to output array
    output[m] = arr[m][n];
 
    // If this is last word of current output sentence, then print
    // the output sentence
    if (m==R-1)
    {
        for (int i=0; i


Java
// Java program to print
// all possible sentences
// from a list of word list
class GFG{
 
static final int R= 3;
static final int C =3;
   
// A recursive function to
// print all possible sentences
// that can be formed 
// from a list of word list
static void printUtil(String [][]arr,
                      int m, int n,
                      String []output)
{
  // Add current word to output array
  output[m] = arr[m][n];
 
  // If this is last word of
  // current output sentence,
  // then print the output sentence
  if (m == R - 1)
  {
    for (int i = 0; i < R; i++)
      System.out.print(output[i] + " ");
    System.out.println();
    return;
  }
 
  // Recur for next row
  for (int i = 0; i < C; i++)
    if (arr[m + 1][i] != "" && m < C)
      printUtil(arr, m + 1, i, output);
}
 
// A wrapper over printUtil()
static void print(String [][]arr)
{
  // Create an array to store sentence
  String []output = new String[R];
 
  // Consider all words for first
  // row as starting points and
  // print all sentences
  for (int i = 0; i < C; i++)
    if (arr[0][i] != "")
      printUtil(arr, 0, i, output);
}
 
// Driver program to test above functions
public static void main(String[] args)
{
  String [][]arr  = {{"you", "we", ""},
                     {"have", "are", ""},
                     {"sleep", "eat", "drink"}};
  print(arr);
}
}
 
// This code is contributed by gauravrajput1


Python3
# Python program recursively print all sentences that can be
# formed from list of word lists
R = 3
C = 3
 
# A recursive function to print all possible sentences that can
# be formed from a list of word list
def printUtil(arr, m, n, output):
 
    # Add current word to output array
    output[m] = arr[m][n]
 
    # If this is last word of current output sentence, then print
    # the output sentence
    if m==R-1:
        for i in range(R):
            print (output[i],end= " ")
        print()
        return
 
    # Recur for next row
    for i in range(C):
        if arr[m+1][i] != "":
            printUtil(arr, m+1, i, output)
 
# A wrapper over printUtil
def printf(arr):
 
    # Create an array to store sentence
    output = [""] * R
 
    # Consider all words for first row as starting
    #  points and print all sentences
    for i in range(C):
        if arr[0][i] != "":
            printUtil(arr, 0, i, output)
 
# Driver program
arr = [ ["you", "we",""],
        ["have", "are",""],
        ["sleep", "eat", "drink"]]
printf(arr)
# This code is contributed by Bhavya Jain


C#
// C# program to print
// all possible sentences
// from a list of word list
using System;
class GFG{
 
static readonly int R= 3;
static readonly int C =3;
   
// A recursive function to
// print all possible sentences
// that can be formed 
// from a list of word list
static void printUtil(String [,]arr,
                      int m, int n,
                      String []output)
{
  // Add current word to output array
  output[m] = arr[m, n];
 
  // If this is last word of
  // current output sentence,
  // then print the output sentence
  if (m == R - 1)
  {
    for (int i = 0; i < R; i++)
      Console.Write(output[i] + " ");
    Console.WriteLine();
    return;
  }
 
  // Recur for next row
  for (int i = 0; i < C; i++)
    if (arr[m + 1, i] != "" && m < C)
      printUtil(arr, m + 1, i, output);
}
 
// A wrapper over printUtil()
static void print(String [,]arr)
{
  // Create an array to store sentence
  String []output = new String[R];
 
  // Consider all words for first
  // row as starting points and
  // print all sentences
  for (int i = 0; i < C; i++)
    if (arr[0, i] != "")
      printUtil(arr, 0, i, output);
}
 
// Driver program to test above functions
public static void Main(String[] args)
{
  String [,]arr  = {{"you", "we", ""},
                    {"have", "are", ""},
                    {"sleep", "eat", "drink"}};
  print(arr);
}
}
 
// This code is contributed by gauravrajput1


输出:

you have sleep
  you have eat
  you have drink
  you are sleep
  you are eat
  you are drink
  we have sleep
  we have eat
  we have drink
  we are sleep
  we are eat
  we are drink