📌  相关文章
📜  子数组/子字符串与子序列以及生成它们的程序

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

子数组/子字符串与子序列以及生成它们的程序

子数组/子字符串

子数组是数组的连续部分。另一个数组中的数组。例如,考虑数组 [1, 2, 3, 4],有 10 个非空子数组。子数组是 (1), (2), (3), (4), (1,2), (2,3), (3,4), (1,2,3), (2,3, 4) 和 (1,2,3,4)。通常,对于大小为 n 的数组/字符串,有n*(n+1)/2 个非空子数组/子字符串。

subseq-vs-子数组

如何生成所有子数组?
我们可以运行两个嵌套循环,外循环选择起始元素,内循环将所选择元素右侧的所有元素视为子数组的结束元素。

C++
/*  C++ code to generate all possible subarrays/subArrays
    Complexity- O(n^3) */
#include
using namespace std;
 
// Prints all subarrays in arr[0..n-1]
void subArray(int arr[], int n)
{
    // Pick starting point
    for (int i=0; i 


Java
// Java program toto generate all possible subarrays/subArrays
//  Complexity- O(n^3) */
 
class Test
{
    static int arr[] = new int[]{1, 2, 3, 4};
     
    // Prints all subarrays in arr[0..n-1]
    static void subArray( int n)
    {
        // Pick starting point
        for (int i=0; i 


Python3
# Python3 code to generate all possible
# subarrays/subArrays
# Complexity- O(n^3)
 
# Prints all subarrays in arr[0..n-1]
def subArray(arr, n):
 
    # Pick starting point
    for i in range(0,n):
 
        # Pick ending point
        for j in range(i,n):
 
            # Print subarray between
            # current starting
            # and ending points
            for k in range(i,j+1):
                print (arr[k],end=" ")
 
            print ("\n",end="")
 
             
# Driver program
arr = [1, 2, 3, 4]
n = len(arr)
print ("All Non-empty Subarrays")
 
subArray(arr, n);
 
# This code is contributed by Shreyanshi.


C#
// C# program toto generate all
// possible subarrays/subArrays
// Complexity- O(n^3)
using System;
 
class GFG
{
    static int []arr = new int[]{1, 2, 3, 4};
     
    // Prints all subarrays in arr[0..n-1]
    static void subArray( int n)
    {
         
        // Pick starting point
        for (int i = 0; i < n; i++)
        {
             
            // Pick ending point
            for (int j = i; j < n; j++)
            {
                 
                // Print subarray between current
                // starting and ending points
                for (int k = i; k <= j; k++)
                    Console.Write(arr[k]+" ");
                    Console.WriteLine("");
            }
        }
    }
     
    // Driver Code
    public static void Main()
    {
        Console.WriteLine("All Non-empty Subarrays");
        subArray(arr.Length);
         
    }
 
}
 
// This code is contributed by Sam007.


PHP


Javascript


C++
/*  C++ code to generate all possible subsequences.
    Time Complexity O(n * 2^n) */
#include
using namespace std;
 
void printSubsequences(int arr[], int n)
{
    /* Number of subsequences is (2**n -1)*/
    unsigned int opsize = pow(2, n);
 
    /* Run from counter 000..1 to 111..1*/
    for (int counter = 1; counter < opsize; counter++)
    {
        for (int j = 0; j < n; j++)
        {
            /* Check if jth bit in the counter is set
                If set then print jth element from arr[] */
            if (counter & (1<


Java
/*  Java code to generate all possible subsequences.
    Time Complexity O(n * 2^n) */
 
import java.math.BigInteger;
 
class Test
{
    static int arr[] = new int[]{1, 2, 3, 4};
     
    static void printSubsequences(int n)
    {
        /* Number of subsequences is (2**n -1)*/
        int opsize = (int)Math.pow(2, n);
      
        /* Run from counter 000..1 to 111..1*/
        for (int counter = 1; counter < opsize; counter++)
        {
            for (int j = 0; j < n; j++)
            {
                /* Check if jth bit in the counter is set
                    If set then print jth element from arr[] */
       
                if (BigInteger.valueOf(counter).testBit(j))
                    System.out.print(arr[j]+" ");
            }
            System.out.println();
        }
    }
     
    // Driver method to test the above function
    public static void main(String[] args)
    {
        System.out.println("All Non-empty Subsequences");
        printSubsequences(arr.length);
    }
}


Python3
# Python3 code to generate all
# possible subsequences.
# Time Complexity O(n * 2 ^ n)
import math
 
def printSubsequences(arr, n) :
 
    # Number of subsequences is (2**n -1)
    opsize = math.pow(2, n)
 
    # Run from counter 000..1 to 111..1
    for counter in range( 1, (int)(opsize)) :
        for j in range(0, n) :
             
            # Check if jth bit in the counter
            # is set If set then print jth
            # element from arr[]
            if (counter & (1<


C#
// C# code to generate all possible subsequences.
// Time Complexity O(n * 2^n)
using System;
 
class GFG{
 
static void printSubsequences(int[] arr, int n)
{
     
    // Number of subsequences is (2**n -1)
    int opsize = (int)Math.Pow(2, n);
     
    // Run from counter 000..1 to 111..1
    for(int counter = 1; counter < opsize; counter++)
    {
        for(int j = 0; j < n; j++)
        {
             
            // Check if jth bit in the counter is set
            // If set then print jth element from arr[]
            if ((counter & (1 << j)) != 0)
                Console.Write(arr[j] + " ");
        }
        Console.WriteLine();
    }
}
 
// Driver Code
static void Main()
{
    int[] arr = { 1, 2, 3, 4 };
    int n = arr.Length;
     
    Console.WriteLine("All Non-empty Subsequences");
     
    printSubsequences(arr, n);
}
}
 
// This code is contributed by divyesh072019


PHP


Javascript


输出:

All Non-empty Subarrays
1 
1 2 
1 2 3 
1 2 3 4 
2 
2 3 
2 3 4 
3 
3 4 
4

时间复杂度:0(n^3)

空间复杂度:0(1)

子序列
子序列是可以通过删除零个或多个元素从另一个序列派生的序列,而不改变剩余元素的顺序。
对于同一个例子,有 15 个子序列。它们是 (1), (2), (3), (4), (1,2), (1,3),(1,4), (2,3), (2,4), (3 ,4), (1,2,3), (1,2,4), (1,3,4), (2,3,4), (1,2,3,4)。更一般地说,我们可以说对于一个大小为 n 的序列,我们总共可以有 ( 2 n -1 ) 个非空子序列。
区分字符串的示例:考虑字符串“geeksforgeeks”和“gks”。 “gks”是“geeksforgeeks”的子序列,但不是子字符串。 “geeks”既是子序列又是子数组。每个子数组都是一个子序列。更具体地说,子序列是子字符串的泛化。

子数组或子串总是连续的,但子序列不一定是连续的。也就是说,子序列不需要占据原始序列中的连续位置。但是我们可以说连续子序列和子数组都是一样的。

如何生成所有子序列?
我们可以使用算法来生成幂集以生成所有子序列。

C++

/*  C++ code to generate all possible subsequences.
    Time Complexity O(n * 2^n) */
#include
using namespace std;
 
void printSubsequences(int arr[], int n)
{
    /* Number of subsequences is (2**n -1)*/
    unsigned int opsize = pow(2, n);
 
    /* Run from counter 000..1 to 111..1*/
    for (int counter = 1; counter < opsize; counter++)
    {
        for (int j = 0; j < n; j++)
        {
            /* Check if jth bit in the counter is set
                If set then print jth element from arr[] */
            if (counter & (1<

Java

/*  Java code to generate all possible subsequences.
    Time Complexity O(n * 2^n) */
 
import java.math.BigInteger;
 
class Test
{
    static int arr[] = new int[]{1, 2, 3, 4};
     
    static void printSubsequences(int n)
    {
        /* Number of subsequences is (2**n -1)*/
        int opsize = (int)Math.pow(2, n);
      
        /* Run from counter 000..1 to 111..1*/
        for (int counter = 1; counter < opsize; counter++)
        {
            for (int j = 0; j < n; j++)
            {
                /* Check if jth bit in the counter is set
                    If set then print jth element from arr[] */
       
                if (BigInteger.valueOf(counter).testBit(j))
                    System.out.print(arr[j]+" ");
            }
            System.out.println();
        }
    }
     
    // Driver method to test the above function
    public static void main(String[] args)
    {
        System.out.println("All Non-empty Subsequences");
        printSubsequences(arr.length);
    }
}

Python3

# Python3 code to generate all
# possible subsequences.
# Time Complexity O(n * 2 ^ n)
import math
 
def printSubsequences(arr, n) :
 
    # Number of subsequences is (2**n -1)
    opsize = math.pow(2, n)
 
    # Run from counter 000..1 to 111..1
    for counter in range( 1, (int)(opsize)) :
        for j in range(0, n) :
             
            # Check if jth bit in the counter
            # is set If set then print jth
            # element from arr[]
            if (counter & (1<

C#

// C# code to generate all possible subsequences.
// Time Complexity O(n * 2^n)
using System;
 
class GFG{
 
static void printSubsequences(int[] arr, int n)
{
     
    // Number of subsequences is (2**n -1)
    int opsize = (int)Math.Pow(2, n);
     
    // Run from counter 000..1 to 111..1
    for(int counter = 1; counter < opsize; counter++)
    {
        for(int j = 0; j < n; j++)
        {
             
            // Check if jth bit in the counter is set
            // If set then print jth element from arr[]
            if ((counter & (1 << j)) != 0)
                Console.Write(arr[j] + " ");
        }
        Console.WriteLine();
    }
}
 
// Driver Code
static void Main()
{
    int[] arr = { 1, 2, 3, 4 };
    int n = arr.Length;
     
    Console.WriteLine("All Non-empty Subsequences");
     
    printSubsequences(arr, n);
}
}
 
// This code is contributed by divyesh072019

PHP


Javascript


输出:

All Non-empty Subsequences
1 
2 
1 2 
3 
1 3 
2 3 
1 2 3 
4 
1 4 
2 4 
1 2 4 
3 4 
1 3 4 
2 3 4 
1 2 3 4

时间复杂度:0(n*(2^n))