给定整数N ,任务是计算长度为N的二进制字符串的数量,以使它们不包含“ 111”作为子字符串。答案可能很大,因此请以10 9 + 7为模输出答案。
例子:
Input: N = 3
Output: 7
All possible substring are “000”, “001”,
“010”, “011”, “100”, “101” and “110”.
“111” is not a valid string.
Input N = 16
Output: 19513
方法:可以使用动态编程来解决此问题。创建一个dp [] []数组,其中dp [i] [j]将存储可能的子字符串的数量,以使1连续出现j次直至第ith个索引。现在,递归关系将是:
dp[i][0] = dp[i – 1][0] + dp[i – 1][1] + dp[i – 1][2]
dp[i][1] = dp[i – 1][0]
dp[i][2] = dp[i – 1][1]
基本情况将是dp [1] [0] = 1 , dp [1] [1] = 1和dp [1] [2] = 0 。现在,所需的字符串将为dp [N] [0] + dp [N] [1] + dp [N] [2] 。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
const long MOD = 1000000007;
// Function to return the count
// of all possible valid strings
long countStrings(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] = number of possible strings
// such that '1' just appeared consecutively
// j times upto the 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 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 = 3;
cout << countStrings(N);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
final static int MOD = 1000000007;
// Function to return the count
// of all possible valid strings
static long countStrings(int N)
{
int i, j;
int dp[][] = new int[N + 1][3];
// Fill 0's in the dp array
for(i = 0; i < N + 1; i++)
{
for(j = 9; j < 3 ; j ++)
{
dp[i][j] = 0;
}
}
// Base cases
dp[1][0] = 1;
dp[1][1] = 1;
dp[1][2] = 0;
for (i = 2; i <= N; i++)
{
// dp[i][j] = number of possible strings
// such that '1' just appeared consecutively
// j times upto the 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 possible cases that
// can appear at the Nth position
int ans = (dp[N][0] + dp[N][1] +
dp[N][2]) % MOD;
return ans;
}
// Driver code
public static void main (String[] args)
{
int N = 3;
System.out.println(countStrings(N));
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 implementation of the approach
MOD = 1000000007
# Function to return the count
# of all possible valid strings
def countStrings(N):
# Initialise and fill 0's in the dp array
dp = [[0] * 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] = number of possible strings
# such that '1' just appeared consecutively
# j times upto the 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 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 = 3
print(countStrings(N))
# This code is contributed by ashutosh450
C#
// C# implementation of the above approach
using System;
class GFG
{
static readonly int MOD = 1000000007;
// Function to return the count
// of all possible valid strings
static long countStrings(int N)
{
int i, j;
int [,]dp = new int[N + 1, 3];
// Fill 0's in the dp array
for(i = 0; i < N + 1; i++)
{
for(j = 9; j < 3; j ++)
{
dp[i, j] = 0;
}
}
// Base cases
dp[1, 0] = 1;
dp[1, 1] = 1;
dp[1, 2] = 0;
for (i = 2; i <= N; i++)
{
// dp[i,j] = number of possible strings
// such that '1' just appeared consecutively
// j times upto the 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 possible cases that
// can appear at the Nth position
int ans = (dp[N, 0] + dp[N, 1] +
dp[N, 2]) % MOD;
return ans;
}
// Driver code
public static void Main (String[] args)
{
int N = 3;
Console.WriteLine(countStrings(N));
}
}
// This code is contributed by Rajput-Ji
Javascript
输出:
7