文本字符串 二进制字符串
Problem statement:
问题陈述:
Given a positive integer N, count all possible distinct binary strings of length N such that there are no consecutive 1's. Output your answer mod 10^9 + 7.
给定一个正整数N ,对长度为N的所有可能的不同二进制字符串进行计数,以使没有连续的1。 输出答案mod 10 ^ 9 + 7 。
Input:
输入:
The first line of input contains an integer T denoting the number of test cases. The description of T test cases follows.
输入的第一行包含一个整数T,表示测试用例的数量。 T测试用例的描述如下。
Each test case contains an integer N representing length of the binary string.
每个测试用例均包含一个代表二进制字符串长度的整数N。
Output:
输出:
Print the count number of binary strings without consecutive 1's of length N.
打印不带连续N的 1的二进制字符串的计数数目。
Constraints:
限制条件:
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
Example:
例:
Number of test cases: 2
Test case 1:
Input:
N: 3
Output:
Number of valid binary sequence: 5
Test case 2:
Input:
N: 2
Output:
Number of valid binary sequence: 3
Explanation:
说明:
Test case 1:
5 strings are (000, 001, 010, 100, 101)
O11, 110, 111 are not allowed.
Test case 2:
3 strings are (00, 01, 10)
11 is not allowed
Solution approach
解决方法
We can solve the above problem by recursion. The idea is place '0' or '1' as the last character and recur for the remaining length. So, we will start by placing 0 as the first character and 1 as the first character.
我们可以通过递归来解决上述问题。 这个想法是将'0'或'1'作为最后一个字符,并在剩余的长度上重复出现。 因此,我们将从将0设为第一个字符并将1设为第一个字符开始。
Result = f(0,1,n) + f(1,1,n)
The function arguments are,
函数参数是
f(last,index,length)
Where last is the last digit, index is the current index and length is the sequence length.
其中last是最后一位数字,index是当前索引,length是序列长度。
So, we have two choice initially,
因此,我们最初有两种选择,
Put 0 at the 0th index and recur for rest of the length, thus last is 0 and index is 1
将0放在第0 个索引处,然后递归其余长度,因此last为0且index为1
Put 1 at the 1st index and recur for rest of the length, thus last is 1 and index is 1
把1第1个索引处和复发的长度的其余部分,从而最后是1和索引为1
Below is the details of the recursive function
以下是递归函数的详细信息
int MOD=1000000007
整数MOD = 1000000007
Function f(int last ,int index,int length)
// base case
if(i==n)
return 1;
if(last==0 )
then we can both use 0 and 1 as our current digit, so,
return (f(0,index+1,n)%MOD + f(1,index+1,n)%MOD)%MOD;
else
only placing 0 is feasible since we can't allow contiguous 1
return f(0,index+1,n)%MOD;
End Function
Since it will generate many over lapping sub-problems, we need to use Dynamic programming to store the already computed sub problems. Below is the memorization approach.
由于它将产生许多重叠子问题,因此我们需要使用动态编程来存储已经计算出的子问题。 下面是记忆方式。
C++ Implementation:
C ++实现:
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
int dp[101][2];
long long int myrecur(int i, int last, int n)
{
// base case
if (i >= n)
return 1;
// memoization by not computing the
// subproblems computed already
if (dp[i][last] != -1)
return dp[i][last];
// store subproblem results
if (last == 0) {
dp[i][last] = (myrecur(i + 1, 0, n) % MOD + myrecur(i + 1, 1, n) % MOD) % MOD;
}
else {
dp[i][last] = myrecur(i + 1, 0, n) % MOD;
}
return dp[i][last];
}
long long int countofSequence(int n)
{
//initialize the dp store
for (int i = 0; i <= n; i++) {
dp[i][0] = -1;
dp[i][1] = -1;
}
return (myrecur(1, 0, n) % MOD + myrecur(1, 1, n) % MOD) % MOD;
}
int main()
{
int t, n, item;
cout << "Enter number of testcases\n";
cin >> t;
for (int i = 0; i < t; i++) {
cout << "Enter sequence length\n";
cin >> n;
cout << "Numbder of possible sequence is: " << countofSequence(n) << endl;
}
return 0;
}
Output:
输出:
Enter number of testcases
3
Enter sequence length
3
Numbder of possible sequence is: 5
Enter sequence length
2
Numbder of possible sequence is: 3
Enter sequence length
55
Numbder of possible sequence is: 435293607
翻译自: https://www.includehelp.com/icp/count-number-of-binary-strings-without-consecutive-1s.aspx
文本字符串 二进制字符串