给定字符串的长度N ,我们必须找到长度为N的特殊字符串的数量。
的字符串被称为特殊字符串,如果它仅由小写字母a和b有两个字符串中的之间至少一个B。由于字符串的数量可能很大,因此请以10 ^ 9 + 7为模数打印。
例子:
Input: N = 2
Output: 3
Explanation :
The number of special string so length 2 are 3 i.e. “ab”, “ba”, “bb”
Input: N = 3
Output: 5
Explanation:
The number of special string so length 3 are 5 i.e. “abb”, “aba”, “bab”, “bba”, “bbb”
方法:
为了解决上述问题,第一个观察结果是:如果整数N为0,那么答案只能是一个空字符串;如果N为1,那么答案是两个字符串“ a”或“ b”,但是如果N的值大于1,则答案等于前两项的和。我们发现我们运行一个循环,并为每个整数i计数长度的特殊字符串的特殊字符串的计数i等于长度i-1的特殊字符串的计数之和长度I-的特殊字符串的数2。将每个整数的值存储在数组中,并返回所需的答案。
下面是上述方法的实现:
C++
// C++ Program to Count the number
// of Special Strings of a given length N
#include
using namespace std;
#define mod 1000000007
// Function to return count of special strings
long count_special(long n)
{
// stores the answer for a
// particular value of n
long fib[n + 1];
// for n = 0 we have empty string
fib[0] = 1;
// for n = 1 we have
// 2 special strings
fib[1] = 2;
for (int i = 2; i <= n; i++) {
// calculate count of special string of length i
fib[i] = (fib[i - 1] % mod + fib[i - 2] % mod) % mod;
}
// fib[n] stores the count
// of special strings of length n
return fib[n];
}
// Driver code
int main()
{
// initialise n
long n = 3;
cout << count_special(n) << endl;
return 0;
}
Java
// Java program to count the number of
// special strings of a given length N
import java.util.*;
class GFG{
static final int mod = 1000000007;
// Function to return count of
// special Strings
static int count_special(int n)
{
// Stores the answer for a
// particular value of n
int []fib = new int[n + 1];
// For n = 0 we have empty String
fib[0] = 1;
// For n = 1 we have
// 2 special Strings
fib[1] = 2;
for(int i = 2; i <= n; i++)
{
// Calculate count of special
// String of length i
fib[i] = (fib[i - 1] % mod +
fib[i - 2] % mod) % mod;
}
// fib[n] stores the count of
// special Strings of length n
return fib[n];
}
// Driver code
public static void main(String[] args)
{
// Initialise n
int n = 3;
System.out.print(count_special(n) + "\n");
}
}
// This code is contributed by sapnasingh4991
Python3
# Python3 program to count the number
# of special strings of a given length N
mod = 1000000007
# Function to return count of
# special strings
def count_special(n):
# Stores the answer for a
# particular value of n
fib = [0 for i in range(n + 1)]
# For n = 0 we have empty string
fib[0] = 1
# For n = 1 we have
# 2 special strings
fib[1] = 2
for i in range(2, n + 1, 1):
# Calculate count of special
# string of length i
fib[i] = (fib[i - 1] % mod +
fib[i - 2] % mod) % mod
# fib[n] stores the count
# of special strings of length n
return fib[n]
# Driver code
if __name__ == '__main__':
# Initialise n
n = 3
print(count_special(n))
# This code is contributed by Bhupendra_Singh
C#
// C# program to count the number of
// special strings of a given length N
using System;
class GFG{
const int mod = 1000000007;
// Function to return count of
// special Strings
static int count_special(int n)
{
// Stores the answer for a
// particular value of n
int []fib = new int[n + 1];
// For n = 0 we have empty String
fib[0] = 1;
// For n = 1 we have
// 2 special Strings
fib[1] = 2;
for(int i = 2; i <= n; i++)
{
// Calculate count of special
// String of length i
fib[i] = (fib[i - 1] % mod +
fib[i - 2] % mod) % mod;
}
// fib[n] stores the count of
// special Strings of length n
return fib[n];
}
// Driver code
public static void Main()
{
// Initialise n
int n = 3;
Console.Write(count_special(n) + "\n");
}
}
// This code is contributed by Nidhi_biet
输出:
5