📌  相关文章
📜  找出长度为 N 且至少有 3 个连续 1 的二进制字符串的个数

📅  最后修改于: 2021-09-22 09:57:18             🧑  作者: Mango

给定一个整数N 。任务是找到长度为N 且至少有 3 个连续 1 的所有可能的不同二进制字符串的数量。
例子:

天真的方法:考虑所有可能的字符串。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function that returns true if s contains
// three consecutive 1's
bool check(string& s)
{
    int n = s.length();
    for (int i = 2; i < n; i++) {
        if (s[i] == '1' && s[i - 1] == '1' && s[i - 2] == '1')
            return 1;
    }
    return 0;
}
 
// Function to return the count
// of required strings
int countStr(int i, string& s)
{
    if (i < 0) {
        if (check(s))
            return 1;
        return 0;
    }
    s[i] = '0';
    int ans = countStr(i - 1, s);
    s[i] = '1';
    ans += countStr(i - 1, s);
    s[i] = '0';
    return ans;
}
 
// Driver code
int main()
{
    int N = 4;
    string s(N, '0');
    cout << countStr(N - 1, s);
 
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
 
class GFG
{
 
// Function that returns true if s contains
// three consecutive 1's
static boolean check(String s)
{
    int n = s.length();
    for (int i = 2; i < n; i++)
    {
        if (s.charAt(i) == '1' &&
          s.charAt(i-1) == '1' &&
          s.charAt(i-2) == '1')
            return true;
    }
    return false;
}
 
// Function to return the count
// of required strings
static int countStr(int i, String s)
{
    if (i < 0)
    {
        if (check(s))
            return 1;
        return 0;
    }
    char[] myNameChars = s.toCharArray();
    myNameChars[i] = '0';
    s = String.valueOf(myNameChars);
 
    int ans = countStr(i - 1, s);
    char[] myChar = s.toCharArray();
    myChar[i] = '1';
    s = String.valueOf(myChar);
 
    ans += countStr(i - 1, s);
    char[]myChar1 = s.toCharArray();
    myChar1[i] = '0';
    s = String.valueOf(myChar1);
 
    return ans;
}
 
// Driver code
public static void main(String args[])
{
    int N = 4;
    String s = "0000";
    System.out.println(countStr(N - 1, s));
}
}
 
// This code is contributed by
// Surendra_Gangwar


Python3
# Python3 implementation of the approach
 
# Function that returns true if s contains
# three consecutive 1's
def check(s) :
    n = len(s);
    for i in range(2, n) :
        if (s[i] == '1' and s[i - 1] == '1' and
                            s[i - 2] == '1') :
            return 1;
 
# Function to return the count
# of required strings
def countStr(i, s) :
     
    if (i < 0) :
        if (check(s)) :
            return 1;
        return 0;
     
    s[i] = '0';
    ans = countStr(i - 1, s);
     
    s[i] = '1';
    ans += countStr(i - 1, s);
     
    s[i] = '0';
    return ans;
 
# Driver code
if __name__ == "__main__" :
    N = 4;
    s = list('0' * N);
     
    print(countStr(N - 1, s));
 
# This code is contributed by Ryuga


C#
// C# implementation of the approach
// value x
using System;
 
class GFG
{
 
// Function that returns true if s contains
// three consecutive 1's
static bool check(String s)
{
    int n = s.Length;
    for (int i = 2; i < n; i++)
    {
        if (s[i] == '1' && s[i - 1] == '1' && s[i - 2] == '1')
            return true;
    }
    return false;
}
 
// Function to return the count
// of required strings
static int countStr(int i, String s)
{
    if (i < 0)
    {
        if (check(s))
            return 1;
        return 0;
    }
    char[] myNameChars = s.ToCharArray();
    myNameChars[i] = '0';
    s = String.Join("", myNameChars);
 
    int ans = countStr(i - 1, s);
    char[] myChar = s.ToCharArray();
    myChar[i] = '1';
    s = String.Join("", myChar);
 
    ans += countStr(i - 1, s);
    char[]myChar1 = s.ToCharArray();
    myChar1[i] = '0';
    s = String.Join("", myChar1);
 
    return ans;
}
 
// Driver code
public static void Main(String []args)
{
    int N = 4;
    String s = "0000";
    Console.WriteLine(countStr(N - 1, s));
}
}
 
/* This code contributed by PrinciRaj1992 */


Javascript


C++
// C++ implementation of the approach
#include 
using namespace std;
 
int n;
 
// Function to return the count
// of required strings
int solve(int i, int x, int dp[][4])
{
    if (i < 0)
        return x == 3;
    if (dp[i][x] != -1)
        return dp[i][x];
 
    // '0' at ith position
    dp[i][x] = solve(i - 1, 0, dp);
 
    // '1' at ith position
    dp[i][x] += solve(i - 1, x + 1, dp);
    return dp[i][x];
}
 
// Driver code
int main()
{
    n = 4;
    int dp[n][4];
 
    for (int i = 0; i < n; i++)
        for (int j = 0; j < 4; j++)
            dp[i][j] = -1;
 
    for (int i = 0; i < n; i++) {
 
        // Base condition:
        // 2^(i+1) because of 0 indexing
        dp[i][3] = (1 << (i + 1));
    }
    cout << solve(n - 1, 0, dp);
 
    return 0;
}


Java
// Java implementation of the above approach
import java.util.*;
 
class GFG
{
 
    static int n;
 
    // Function to return the count
    // of required strings
    static int solve(int i, int x, int dp[][])
    {
        if (i < 0)
        {
            return x == 3 ? 1 : 0;
        }
        if (dp[i][x] != -1)
        {
            return dp[i][x];
        }
 
        // '0' at ith position
        dp[i][x] = solve(i - 1, 0, dp);
 
        // '1' at ith position
        dp[i][x] += solve(i - 1, x + 1, dp);
        return dp[i][x];
    }
 
    // Driver code
    public static void main(String[] args)
    {
        n = 4;
        int dp[][] = new int[n][4];
 
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                dp[i][j] = -1;
            }
        }
 
        for (int i = 0; i < n; i++)
        {
 
            // Base condition:
            // 2^(i+1) because of 0 indexing
            dp[i][3] = (1 << (i + 1));
        }
        System.out.print(solve(n - 1, 0, dp));
    }
}
 
// This code has been contributed by 29AjayKumar


Python 3
# Python 3 implementation of the approach
 
# Function to return the count
# of required strings
def solve(i, x, dp):
    if (i < 0):
        return x == 3
    if (dp[i][x] != -1):
        return dp[i][x]
 
    # '0' at ith position
    dp[i][x] = solve(i - 1, 0, dp)
 
    # '1' at ith position
    dp[i][x] += solve(i - 1, x + 1, dp)
    return dp[i][x]
 
 
# Driver code
if __name__ == "__main__":
 
    n = 4;
    dp = [[0 for i in range(n)] for j in range(4)]
 
    for i in range(n):
        for j in range(4):
            dp[i][j] = -1
 
    for i in range(n) :
 
        # Base condition:
        # 2^(i+1) because of 0 indexing
        dp[i][3] = (1 << (i + 1))
     
    print(solve(n - 1, 0, dp))
 
# This code is contributed by ChitraNayal


C#
// C# implementation of the above approach
using System;
 
class GFG
{
 
    static int n;
 
    // Function to return the count
    // of required strings
    static int solve(int i, int x, int [,]dp)
    {
        if (i < 0)
        {
            return x == 3 ? 1 : 0;
        }
        if (dp[i,x] != -1)
        {
            return dp[i,x];
        }
 
        // '0' at ith position
        dp[i,x] = solve(i - 1, 0, dp);
 
        // '1' at ith position
        dp[i,x] += solve(i - 1, x + 1, dp);
        return dp[i,x];
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        n = 4;
        int [,]dp = new int[n, 4];
 
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                dp[i, j] = -1;
            }
        }
 
        for (int i = 0; i < n; i++)
        {
 
            // Base condition:
            // 2^(i+1) because of 0 indexing
            dp[i,3] = (1 << (i + 1));
        }
        Console.Write(solve(n - 1, 0, dp));
    }
}
 
// This code contributed by Rajput-Ji


Javascript


输出:

3

时间复杂度: O(2 N )
空间复杂度: O(N),因为递归堆栈空间。
高效的方法:我们使用动态规划来计算字符串的数量。
dp 的状态: dp(i, x) :表示在位置 i + 1 到 i + x 处有 x 个连续 1 的长度为 i 的字符串的数量。
重复: dp(i, x) = dp(i – 1, 0) + dp(i – 1, x + 1)
重复是基于这样一个事实,即字符串可以在位置 i 处为“0”或“1”。

  1. 如果它在位置 i 有一个“0”,那么对于 x = 0 的第 (i-1) 个位置值。
  2. 如果它在位置 i 有一个“1”,则 x 的第 (i-1) 个位置值 = 位置 i + 1 处的 x 值。

基本条件: dp(i, 3) = 2 i 。因为一旦你有 3 个连续的“1”,你就不会关心在字符串的1、2…i 位置有什么字符,因为所有的字符串都是有效的。
下面是上述方法的实现:

C++

// C++ implementation of the approach
#include 
using namespace std;
 
int n;
 
// Function to return the count
// of required strings
int solve(int i, int x, int dp[][4])
{
    if (i < 0)
        return x == 3;
    if (dp[i][x] != -1)
        return dp[i][x];
 
    // '0' at ith position
    dp[i][x] = solve(i - 1, 0, dp);
 
    // '1' at ith position
    dp[i][x] += solve(i - 1, x + 1, dp);
    return dp[i][x];
}
 
// Driver code
int main()
{
    n = 4;
    int dp[n][4];
 
    for (int i = 0; i < n; i++)
        for (int j = 0; j < 4; j++)
            dp[i][j] = -1;
 
    for (int i = 0; i < n; i++) {
 
        // Base condition:
        // 2^(i+1) because of 0 indexing
        dp[i][3] = (1 << (i + 1));
    }
    cout << solve(n - 1, 0, dp);
 
    return 0;
}

Java

// Java implementation of the above approach
import java.util.*;
 
class GFG
{
 
    static int n;
 
    // Function to return the count
    // of required strings
    static int solve(int i, int x, int dp[][])
    {
        if (i < 0)
        {
            return x == 3 ? 1 : 0;
        }
        if (dp[i][x] != -1)
        {
            return dp[i][x];
        }
 
        // '0' at ith position
        dp[i][x] = solve(i - 1, 0, dp);
 
        // '1' at ith position
        dp[i][x] += solve(i - 1, x + 1, dp);
        return dp[i][x];
    }
 
    // Driver code
    public static void main(String[] args)
    {
        n = 4;
        int dp[][] = new int[n][4];
 
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                dp[i][j] = -1;
            }
        }
 
        for (int i = 0; i < n; i++)
        {
 
            // Base condition:
            // 2^(i+1) because of 0 indexing
            dp[i][3] = (1 << (i + 1));
        }
        System.out.print(solve(n - 1, 0, dp));
    }
}
 
// This code has been contributed by 29AjayKumar

Python3

# Python 3 implementation of the approach
 
# Function to return the count
# of required strings
def solve(i, x, dp):
    if (i < 0):
        return x == 3
    if (dp[i][x] != -1):
        return dp[i][x]
 
    # '0' at ith position
    dp[i][x] = solve(i - 1, 0, dp)
 
    # '1' at ith position
    dp[i][x] += solve(i - 1, x + 1, dp)
    return dp[i][x]
 
 
# Driver code
if __name__ == "__main__":
 
    n = 4;
    dp = [[0 for i in range(n)] for j in range(4)]
 
    for i in range(n):
        for j in range(4):
            dp[i][j] = -1
 
    for i in range(n) :
 
        # Base condition:
        # 2^(i+1) because of 0 indexing
        dp[i][3] = (1 << (i + 1))
     
    print(solve(n - 1, 0, dp))
 
# This code is contributed by ChitraNayal

C#

// C# implementation of the above approach
using System;
 
class GFG
{
 
    static int n;
 
    // Function to return the count
    // of required strings
    static int solve(int i, int x, int [,]dp)
    {
        if (i < 0)
        {
            return x == 3 ? 1 : 0;
        }
        if (dp[i,x] != -1)
        {
            return dp[i,x];
        }
 
        // '0' at ith position
        dp[i,x] = solve(i - 1, 0, dp);
 
        // '1' at ith position
        dp[i,x] += solve(i - 1, x + 1, dp);
        return dp[i,x];
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        n = 4;
        int [,]dp = new int[n, 4];
 
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                dp[i, j] = -1;
            }
        }
 
        for (int i = 0; i < n; i++)
        {
 
            // Base condition:
            // 2^(i+1) because of 0 indexing
            dp[i,3] = (1 << (i + 1));
        }
        Console.Write(solve(n - 1, 0, dp));
    }
}
 
// This code contributed by Rajput-Ji

Javascript


输出:
3

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程