给定数字n,请计算n个长度为1的长度字符串的数目。
例子:
Input : n = 2
Output : 1
There are 4 strings of length 2, the
strings are 00, 01, 10 and 11. Only the
string 11 has consecutive 1's.
Input : n = 3
Output : 3
There are 8 strings of length 3, the
strings are 000, 001, 010, 011, 100,
101, 110 and 111. The strings with
consecutive 1's are 011, 110 and 111.
Input : n = 5
Output : 19
可以使用动态编程解决不带连续1的字符串计数的相反问题(请参阅此处的解决方案)。我们可以使用该解决方案,并通过以下步骤找到所需的数量。
- 使用此处讨论的方法计算不带连续1的二进制字符串的数量。将此计数设为c 。
- 连续的1的所有可能的二进制字符串的计数为2 ^ n,其中n为输入长度。
- 连续1的二进制字符串总数为2 ^ n – c。
以下是上述步骤的执行。
C++
// C++ program to count all distinct
// binary strings with two consecutive 1's
#include
using namespace std;
// Returns count of n length binary
// strings with consecutive 1's
int countStrings(int n)
{
// Count binary strings without consecutive 1's.
// See the approach discussed on be
// ( http://goo.gl/p8A3sW )
int a[n], b[n];
a[0] = b[0] = 1;
for (int i = 1; i < n; i++)
{
a[i] = a[i - 1] + b[i - 1];
b[i] = a[i - 1];
}
// Subtract a[n-1]+b[n-1] from 2^n
return (1 << n) - a[n - 1] - b[n - 1];
}
// Driver code
int main()
{
cout << countStrings(5) << endl;
return 0;
}
Java
// Java program to count all distinct
// binary strings with two consecutive 1's
class GFG {
// Returns count of n length binary
// strings with consecutive 1's
static int countStrings(int n)
{
// Count binary strings without consecutive 1's.
// See the approach discussed on be
// ( http://goo.gl/p8A3sW )
int a[] = new int[n], b[] = new int[n];
a[0] = b[0] = 1;
for (int i = 1; i < n; i++) {
a[i] = a[i - 1] + b[i - 1];
b[i] = a[i - 1];
}
// Subtract a[n-1]+b[n-1]
from 2 ^ n return (1 << n) - a[n - 1] - b[n - 1];
}
// Driver code
public static void main(String args[])
{
System.out.println(countStrings(5));
}
}
// This code is contributed by Nikita tiwari.
Python 3
# Python 3 program to count all
# distinct binary strings with
# two consecutive 1's
# Returns count of n length
# binary strings with
# consecutive 1's
def countStrings(n):
# Count binary strings without
# consecutive 1's.
# See the approach discussed on be
# ( http://goo.gl/p8A3sW )
a = [0] * n
b = [0] * n
a[0] = b[0] = 1
for i in range(1, n):
a[i] = a[i - 1] + b[i - 1]
b[i] = a[i - 1]
# Subtract a[n-1]+b[n-1] from 2^n
return (1 << n) - a[n - 1] - b[n - 1]
# Driver code
print(countStrings(5))
# This code is contributed
# by Nikita tiwari.
C#
// program to count all distinct
// binary strings with two
// consecutive 1's
using System;
class GFG {
// Returns count of n length
// binary strings with
// consecutive 1's
static int countStrings(int n)
{
// Count binary strings without
// consecutive 1's.
// See the approach discussed on
// ( http://goo.gl/p8A3sW )
int[] a = new int[n];
int[] b = new int[n];
a[0] = b[0] = 1;
for (int i = 1; i < n; i++)
{
a[i] = a[i - 1] + b[i - 1];
b[i] = a[i - 1];
}
// Subtract a[n-1]+b[n-1]
// from 2^n
return (1 << n) - a[n - 1] - b[n - 1];
}
// Driver code
public static void Main()
{
Console.WriteLine(countStrings(5));
}
}
// This code is contributed by Anant Agarwal.
PHP
Javascript
输出
19
优化:
上述解决方案的时间复杂度为O(n)。我们可以优化上述解决方案以在O(Logn)中工作。
如果我们仔细研究不带连续1的字符串计数模式,我们可以观察到,当n> = 1时,计数实际上是第(n + 2)个斐波那契数。斐波那契数为0、1、1、2, 3,5,8,13,21,34,55,89,141,…。
n = 1, count = 0 = 21 - fib(3)
n = 2, count = 1 = 22 - fib(4)
n = 3, count = 3 = 23 - fib(5)
n = 4, count = 8 = 24 - fib(6)
n = 5, count = 19 = 25 - fib(7)
................