给定一个整数N ,任务是计算可能的二进制字符串的数量,这样就没有全 1 的长度≥3 的子字符串。这个计数可能会变得非常大,所以打印答案模10 9 + 7 。
例子:
Input: N = 4
Output: 13
All possible valid strings are 0000, 0001, 0010, 0100,
1000, 0101, 0011, 1010, 1001, 0110, 1100, 1101 and 1011.
Input: N = 2
Output: 4
方法:对于从1到N 的每个值,唯一需要的字符串是其中 ‘1’ 连续出现两次、一次或零次的子串的数量。这可以从2到N递归计算。动态规划可用于记忆,其中dp[i][j]将存储可能字符串的数量,使得1连续出现j次直到第i个索引, j将是0, 1, 2, …, i (可能从1到N不等)。
dp[i][0] = dp[i – 1][0] + dp[i – 1][1] + dp[i – 1][2]如在i位置, 0将被放入。
dp[i][1] = dp[i – 1][0]因为在第(i – 1)个位置没有1 ,所以我们取这个值。
dp[i][2] = dp[i – 1][1]因为第一个1出现在第(i – 1)个位置(连续)所以我们直接取那个值。
基本情况是长度为 1 的字符串,即dp[1][0] = 1 , dp[1][1] = 1 , dp[1][2] = 0 。因此,找到所有值dp[N][0] + dp[N][1] + dp[N][2]以及在第N个位置的所有可能情况的总和。
下面是上述方法的实现:
CPP
// C++ implementation of the approach
#include
using namespace std;
const long MOD = 1000000007;
// Function to return the count of
// all possible binary strings
long countStr(long N)
{
long dp[N + 1][3];
// Fill 0's in the dp array
memset(dp, 0, sizeof(dp));
// Base cases
dp[1][0] = 1;
dp[1][1] = 1;
dp[1][2] = 0;
for (int i = 2; i <= N; i++) {
// dp[i][j] is the number of possible
// strings such that '1' just appeared
// consecutively j times upto ith index
dp[i][0] = (dp[i - 1][0] + dp[i - 1][1]
+ dp[i - 1][2])
% MOD;
// Taking previously calculated value
dp[i][1] = dp[i - 1][0] % MOD;
dp[i][2] = dp[i - 1][1] % MOD;
}
// Taking all the possible cases that
// can appear at the Nth position
long ans = (dp[N][0] + dp[N][1] + dp[N][2]) % MOD;
return ans;
}
// Driver code
int main()
{
long N = 8;
cout << countStr(N);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
final static long MOD = 1000000007;
// Function to return the count of
// all possible binary strings
static long countStr(int N)
{
long dp[][] = new long[N + 1][3];
// Fill 0's in the dp array
//memset(dp, 0, sizeof(dp));
// Base cases
dp[1][0] = 1;
dp[1][1] = 1;
dp[1][2] = 0;
for (int i = 2; i <= N; i++)
{
// dp[i][j] is the number of possible
// strings such that '1' just appeared
// consecutively j times upto ith index
dp[i][0] = (dp[i - 1][0] + dp[i - 1][1]
+ dp[i - 1][2]) % MOD;
// Taking previously calculated value
dp[i][1] = dp[i - 1][0] % MOD;
dp[i][2] = dp[i - 1][1] % MOD;
}
// Taking all the possible cases that
// can appear at the Nth position
long ans = (dp[N][0] + dp[N][1] + dp[N][2]) % MOD;
return ans;
}
// Driver code
public static void main (String[] args)
{
int N = 8;
System.out.println(countStr(N));
}
}
// This code is contributed by AnkitRai01
Python
# Python3 implementation of the approach
MOD = 1000000007
# Function to return the count of
# all possible binary strings
def countStr(N):
dp = [[0 for i in range(3)] for i in range(N + 1)]
# Base cases
dp[1][0] = 1
dp[1][1] = 1
dp[1][2] = 0
for i in range(2, N + 1):
# dp[i][j] is the number of possible
# strings such that '1' just appeared
# consecutively j times upto ith index
dp[i][0] = (dp[i - 1][0] + dp[i - 1][1] +
dp[i - 1][2]) % MOD
# Taking previously calculated value
dp[i][1] = dp[i - 1][0] % MOD
dp[i][2] = dp[i - 1][1] % MOD
# Taking all the possible cases that
# can appear at the Nth position
ans = (dp[N][0] + dp[N][1] + dp[N][2]) % MOD
return ans
# Driver code
if __name__ == '__main__':
N = 8
print(countStr(N))
# This code is contributed by mohit kumar 29
C#
// C# implementation of the approach
using System;
class GFG
{
static long MOD = 1000000007;
// Function to return the count of
// all possible binary strings
static long countStr(int N)
{
long [,]dp = new long[N + 1, 3];
// Base cases
dp[1, 0] = 1;
dp[1, 1] = 1;
dp[1, 2] = 0;
for (int i = 2; i <= N; i++)
{
// dp[i,j] is the number of possible
// strings such that '1' just appeared
// consecutively j times upto ith index
dp[i, 0] = (dp[i - 1, 0] + dp[i - 1, 1]
+ dp[i - 1, 2]) % MOD;
// Taking previously calculated value
dp[i, 1] = dp[i - 1, 0] % MOD;
dp[i, 2] = dp[i - 1, 1] % MOD;
}
// Taking all the possible cases that
// can appear at the Nth position
long ans = (dp[N, 0] + dp[N, 1] + dp[N, 2]) % MOD;
return ans;
}
// Driver code
public static void Main ()
{
int N = 8;
Console.WriteLine(countStr(N));
}
}
// This code is contributed by AnkitRai01
Javascript
149
时间复杂度: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。