给定两个整数N和K ,任务是找到由长度为N的小写字母组成的不同字符串的数量,这些字母最多可以由K个连续元音组成。由于答案可能太大,打印answer%1000000007 。
Input: N = 1, K = 0
Output: 21
Explanation: All the 21 consonants are there which has 0 contiguous vowels and are of length 1.
Input: N = 1, K = 1
Output: 26
方法:解决这个问题的思路是基于动态规划。请按照以下步骤解决问题:
- 让DP [i] [j]是的办法,使长度i的不同的字符串,其中字符串的最后Ĵ字符是元音数量。
- 所以动态规划的状态是:
- 如果j = 0 ,则dp[i][j] = (dp[i-1][0] + dp[i-1][1] +……+ dp[i-1][K])*21 (由整数变量sum 表示) 因为最后添加的字符应该是辅音,而不管它在先前状态中的值如何,只有j的值将变为0 。
- 如果i
则dp[i][j] = 0 。因为不可能创建一个包含j个元音并且长度小于j的字符串。 - 如果我== j时,DP [i] [j] = 5 I,因为在字符串中的字符数等于元音的数量,因此所有字符应为元音。
- 如果j 那么DP [i] [j] = DP [I-1] [j-1] * 5,因为长度i与去年Ĵ字符的元音的字符串可以由仅当最后一个字符是元音和长度为i-1 的字符串将最后j – 1 个字符作为元音。
- 打印dp[n][0] + dp[n][1] + …… + dp[n][K] 之和作为答案。
下面是上述方法的实现
C++
// C++ program for the above approach
#include
using namespace std;
// Power function to calculate
// long powers with mod
long long int power(long long int x,
long long int y,
long long int p)
{
long long int res = 1ll;
x = x % p;
if (x == 0)
return 0;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
// Function for finding number of ways to
// create string with length N and atmost
// K contiguous vowels
int kvowelwords(int N, int K)
{
long long int i, j;
long long int MOD = 1000000007;
// Array dp to store number of ways
long long int dp[N + 1][K + 1] = { 0 };
long long int sum = 1;
for (i = 1; i <= N; i++) {
// dp[i][0] = (dp[i-1][0]+dp[i-1][1]..dp[i-1][k])*21
dp[i][0] = sum * 21;
dp[i][0] %= MOD;
// Now setting sum to be dp[i][0]
sum = dp[i][0];
for (j = 1; j <= K; j++) {
// If j>i, no ways are possible to create
// a string with length i and vowel j
if (j > i)
dp[i][j] = 0;
else if (j == i) {
// If j = i all the character should
// be vowel
dp[i][j] = power(5ll, i, MOD);
}
else {
// dp[i][j] relation with dp[i-1][j-1]
dp[i][j] = dp[i - 1][j - 1] * 5;
}
dp[i][j] %= MOD;
// Adding dp[i][j] in the sum
sum += dp[i][j];
sum %= MOD;
}
}
return sum;
}
// Driver Program
int main()
{
// Input
int N = 3;
int K = 3;
// Function Call
cout << kvowelwords(N, K) << endl;
return 0;
}
Java
// Java program for the above approach
class GFG{
// Power function to calculate
// long powers with mod
static int power(int x, int y, int p)
{
int res = 1;
x = x % p;
if (x == 0)
return 0;
while (y > 0)
{
if ((y & 1) != 0)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
// Function for finding number of ways to
// create string with length N and atmost
// K contiguous vowels
static int kvowelwords(int N, int K)
{
int i, j;
int MOD = 1000000007;
// Array dp to store number of ways
int[][] dp = new int[N + 1][K + 1] ;
int sum = 1;
for(i = 1; i <= N; i++)
{
// dp[i][0] = (dp[i-1][0]+dp[i-1][1]..dp[i-1][k])*21
dp[i][0] = sum * 21;
dp[i][0] %= MOD;
// Now setting sum to be dp[i][0]
sum = dp[i][0];
for(j = 1; j <= K; j++)
{
// If j>i, no ways are possible to create
// a string with length i and vowel j
if (j > i)
dp[i][j] = 0;
else if (j == i)
{
// If j = i all the character should
// be vowel
dp[i][j] = power(5, i, MOD);
}
else
{
// dp[i][j] relation with dp[i-1][j-1]
dp[i][j] = dp[i - 1][j - 1] * 5;
}
dp[i][j] %= MOD;
// Adding dp[i][j] in the sum
sum += dp[i][j];
sum %= MOD;
}
}
return sum;
}
// Driver Code
public static void main(String[] args)
{
// Input
int N = 3;
int K = 3;
// Function Call
System.out.println( kvowelwords(N, K));
}
}
// This code is contributed by target_2
Python3
# Python3 program for the above approach
# Power function to calculate
# long powers with mod
def power(x, y, p):
res = 1
x = x % p
if (x == 0):
return 0
while (y > 0):
if (y & 1):
res = (res * x) % p
y = y >> 1
x = (x * x) % p
return res
# Function for finding number of ways to
# create string with length N and atmost
# K contiguous vowels
def kvowelwords(N, K):
i, j = 0, 0
MOD = 1000000007
# Array dp to store number of ways
dp = [[0 for i in range(K + 1)]
for i in range(N + 1)]
sum = 1
for i in range(1, N + 1):
#dp[i][0] = (dp[i-1][0]+dp[i-1][1]..dp[i-1][k])*21
dp[i][0] = sum * 21
dp[i][0] %= MOD
# Now setting sum to be dp[i][0]
sum = dp[i][0]
for j in range(1, K + 1):
# If j>i, no ways are possible to create
# a string with length i and vowel j
if (j > i):
dp[i][j] = 0
elif (j == i):
# If j = i all the character should
# be vowel
dp[i][j] = power(5, i, MOD)
else:
# dp[i][j] relation with dp[i-1][j-1]
dp[i][j] = dp[i - 1][j - 1] * 5
dp[i][j] %= MOD
# Adding dp[i][j] in the sum
sum += dp[i][j]
sum %= MOD
return sum
# Driver Code
if __name__ == '__main__':
# Input
N = 3
K = 3
# Function Call
print (kvowelwords(N, K))
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Power function to calculate
// long powers with mod
static int power(int x, int y, int p)
{
int res = 1;
x = x % p;
if (x == 0)
return 0;
while (y > 0)
{
if ((y & 1) != 0)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
// Function for finding number of ways to
// create string with length N and atmost
// K contiguous vowels
static int kvowelwords(int N, int K)
{
int i, j;
int MOD = 1000000007;
// Array dp to store number of ways
int[,] dp = new int[N + 1, K + 1];
int sum = 1;
for(i = 1; i <= N; i++)
{
// dp[i][0] = (dp[i-1, 0]+dp[i-1, 1]..dp[i-1][k])*21
dp[i, 0] = sum * 21;
dp[i, 0] %= MOD;
// Now setting sum to be dp[i][0]
sum = dp[i, 0];
for(j = 1; j <= K; j++)
{
// If j>i, no ways are possible to create
// a string with length i and vowel j
if (j > i)
dp[i, j] = 0;
else if (j == i)
{
// If j = i all the character should
// be vowel
dp[i, j] = power(5, i, MOD);
}
else
{
// dp[i][j] relation with dp[i-1][j-1]
dp[i, j] = dp[i - 1, j - 1] * 5;
}
dp[i, j] %= MOD;
// Adding dp[i][j] in the sum
sum += dp[i, j];
sum %= MOD;
}
}
return sum;
}
// Driver Code
public static void Main()
{
// Input
int N = 3;
int K = 3;
// Function Call
Console.Write(kvowelwords(N, K));
}
}
// This code is contributed by code_hunt
Javascript
输出:
17576
时间复杂度: O(N×K)
辅助空间: O(N×K)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。