📜  计算给定长度 N 的特殊字符串的数量

📅  最后修改于: 2021-09-03 14:59:17             🧑  作者: Mango

给定字符串的长度 N ,我们必须找到长度为 N 的特殊字符串的数量。
如果一个字符串仅由小写字母 a 和 b 组成,并且字符串中的两个 a 之间至少有一个 b,则该字符串称为特殊字符串。由于字符串的数量可能非常大,因此将其打印为模 10^9+7。
例子:

方法:
为了解决上面提到的问题,第一个观察结果是如果整数 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


Javascript


输出:
5

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live