📌  相关文章
📜  计算由“ 11”组成的N长度二进制字符串作为子字符串

📅  最后修改于: 2021-06-25 12:22:20             🧑  作者: Mango

给定正整数N ,任务是查找长度为N的二进制字符串的数目,该二进制字符串包含“ 11”作为子字符串。

例子:

方法:这个想法是基于以下发现,得出以“ 11”作为从01开始的二进制表示的子字符串的可能性的数目:

  • 如果第一位为0 ,则起始位对以“ 11”作为子字符串的字符串无效。因此,剩余(N – 1)的比特具有以形成具有“11”作为一个子字符串的字符串。
  • 如果第一位为1 ,随后的位也为1 ,则存在2个(N – 2)个字符串,其中“ 11”作为子字符串。
  • 如果第一位是1,但以下的位为0,则具有“11”作为一个子字符串可以与剩余的(N – 2)形成的字符串。
  • 因此,生成所有长度为N的二进制字符串的递归关系为:

请按照以下步骤解决问题:

  • 初始化一个大小为(N + 1)的数组,例如dp [] ,并将dp [0]分配为0 ,将dp [1]分配为0
  • 预先计算的2的前N个功率并将其存储在一个阵列,说功率[]。
  • [2,N]范围内进行迭代并将dp [i]更新为(dp [i – 1] + dp [i – 2] + power [i – 2])
  • 完成上述步骤后,打印dp [N]的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to count binary strings
// of length N having substring "11"
void binaryStrings(int N)
{
    // Initialize dp[] of size N + 1
    int dp[N + 1];
 
    // Base Cases
    dp[0] = 0;
    dp[1] = 0;
 
    // Stores the first N powers of 2
    int power[N + 1];
    power[0] = 1;
 
    // Generate
    for (int i = 1; i <= N; i++) {
        power[i] = 2 * power[i - 1];
    }
 
    // Iterate over the range [2, N]
    for (int i = 2; i <= N; i++) {
        dp[i] = dp[i - 1]
                + dp[i - 2]
                + power[i - 2];
    }
 
    // Print total count of substrings
    cout << dp[N];
}
 
// Driver Code
int main()
{
    int N = 12;
    binaryStrings(N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to count binary strings
// of length N having substring "11"
static void binaryStrings(int N)
{
     
    // Initialize dp[] of size N + 1
    int[] dp = new int[N + 1];
 
    // Base Cases
    dp[0] = 0;
    dp[1] = 0;
 
    // Stores the first N powers of 2
    int[] power = new int[N + 1];
    power[0] = 1;
 
    // Generate
    for(int i = 1; i <= N; i++)
    {
        power[i] = 2 * power[i - 1];
    }
 
    // Iterate over the range [2, N]
    for(int i = 2; i <= N; i++)
    {
        dp[i] = dp[i - 1] + dp[i - 2] + power[i - 2];
    }
     
    // Print total count of substrings
    System.out.println(dp[N]);
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 12;
     
    binaryStrings(N);
}
}
 
// This code is contributed by ukasp


Python3
# Python3 program for the above approach
 
# Function to count binary strings
# of length N having substring "11"
def binaryStrings(N):
     
    # Initialize dp[] of size N + 1
    dp = [0]*(N + 1)
 
    # Base Cases
    dp[0] = 0
    dp[1] = 0
 
    # Stores the first N powers of 2
    power = [0]*(N + 1)
    power[0] = 1
 
    # Generate
    for i in range(1, N + 1):
        power[i] = 2 * power[i - 1]
 
    # Iterate over the range [2, N]
    for i in range(2, N + 1):
        dp[i] = dp[i - 1] + dp[i - 2] + power[i - 2]
 
    # Prtotal count of substrings
    print (dp[N])
 
# Driver Code
if __name__ == '__main__':
    N = 12
    binaryStrings(N)
 
    # This code is contributed by mohit kumar 29.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
 
  // Function to count binary strings
  // of length N having substring "11"
  static void binaryStrings(int N)
  {
 
    // Initialize dp[] of size N + 1
    int []dp = new int[N + 1];
 
    // Base Cases
    dp[0] = 0;
    dp[1] = 0;
 
    // Stores the first N powers of 2
    int []power = new int[N + 1];
    power[0] = 1;
 
    // Generate
    for (int i = 1; i <= N; i++) {
      power[i] = 2 * power[i - 1];
    }
 
    // Iterate over the range [2, N]
    for (int i = 2; i <= N; i++) {
      dp[i] = dp[i - 1]
        + dp[i - 2]
        + power[i - 2];
    }
 
    // Print total count of substrings
    Console.WriteLine(dp[N]);
  }
 
  // Driver Code
  public static void Main()
  {
    int N = 12;
    binaryStrings(N);
  }
}
 
// This code is contributed by bgangwar59.


Javascript


输出:
3719

时间复杂度: O(N)
辅助空间: O(N)