给定整数N ,任务是计算由以下条件可生成的,由小写元音组成的N长度字符串的数量:
- 每个“ a”后面只能有一个“ e” 。
- 每个“ e”后面只能有一个“ a”或“ i”。
- 每个“ i”后面不能跟有另一个“ i” 。
- 每个“ o”都只能跟一个“ i”或“ u” 。
- 每个“ u”后面只能有一个“ a” 。
例子:
Input: N = 1
Output: 5
Explanation: All strings that can be formed are: “a”, “e”, “i”, “o” and “u”.
Input: N = 2
Output: 10
Explanation: All strings that can be formed are: “ae”, “ea”, “ei”, “ia”, “ie”, “io”, “iu”, “oi”, “ou” and “ua”.
方法:解决此问题的想法是将其可视化为图问题。根据给定的规则,可以构造一个有向图,其中u到v的边表示v可以立即在u之后写入结果字符串。问题减少到在构造的有向图中找到N长度路径的数量。请按照以下步骤解决问题:
- 让元音a,e,i,o,u分别编号为0、1、2、3、4,并使用给定图中显示的相关性,将图转换为邻接表关系,其中索引表示元音并且该索引处的列表表示从该索引到列表中给定字符的边缘。
- 初始化一个2D数组dp [N +1] [5] ,其中dp [N] [char]表示长度为N的有向路径的数量,该路径终止于特定的顶点char 。
- 初始化DP [I] [炭]对于所有的字符作为1中,由于长度为1的字符串将仅由所述字符串中一个元音的。
- 对于所有可能的长度,假设i ,使用变量u遍历有向边并执行以下步骤:
- 将dp [i + 1] [u]的值更新为0 。
- 遍历节点u的邻接表,并将dp [i] [u]的值增加dp [i] [v] ,该值存储所有值的总和,从而使从节点u到节点v的方向是有向的。
- 完成上述步骤后,所有值dp [N] [i]的和(其中i属于范围[0,5))将得出元音排列的总数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the number of
// vowel permutations possible
int countVowelPermutation(int n)
{
// To avoid the large output value
int MOD = (int)(1e9 + 7);
// Initialize 2D dp array
long dp[n + 1][5];
// Initialize dp[1][i] as 1 since
// string of length 1 will consist
// of only one vowel in the string
for(int i = 0; i < 5; i++)
{
dp[1][i] = 1;
}
// Directed graph using the
// adjacency matrix
vector> relation = {
{ 1 }, { 0, 2 },
{ 0, 1, 3, 4 },
{ 2, 4 }, { 0 }
};
// Iterate over the range [1, N]
for(int i = 1; i < n; i++)
{
// Traverse the directed graph
for(int u = 0; u < 5; u++)
{
dp[i + 1][u] = 0;
// Traversing the list
for(int v : relation[u])
{
// Update dp[i + 1][u]
dp[i + 1][u] += dp[i][v] % MOD;
}
}
}
// Stores total count of permutations
long ans = 0;
for(int i = 0; i < 5; i++)
{
ans = (ans + dp[n][i]) % MOD;
}
// Return count of permutations
return (int)ans;
}
// Driver code
int main()
{
int N = 2;
cout << countVowelPermutation(N);
}
// This code is contributed by Mohit kumar 29
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG {
// Function to find the number of
// vowel permutations possible
public static int
countVowelPermutation(int n)
{
// To avoid the large output value
int MOD = (int)(1e9 + 7);
// Initialize 2D dp array
long[][] dp = new long[n + 1][5];
// Initialize dp[1][i] as 1 since
// string of length 1 will consist
// of only one vowel in the string
for (int i = 0; i < 5; i++) {
dp[1][i] = 1;
}
// Directed graph using the
// adjacency matrix
int[][] relation = new int[][] {
{ 1 }, { 0, 2 },
{ 0, 1, 3, 4 },
{ 2, 4 }, { 0 }
};
// Iterate over the range [1, N]
for (int i = 1; i < n; i++) {
// Traverse the directed graph
for (int u = 0; u < 5; u++) {
dp[i + 1][u] = 0;
// Traversing the list
for (int v : relation[u]) {
// Update dp[i + 1][u]
dp[i + 1][u] += dp[i][v] % MOD;
}
}
}
// Stores total count of permutations
long ans = 0;
for (int i = 0; i < 5; i++) {
ans = (ans + dp[n][i]) % MOD;
}
// Return count of permutations
return (int)ans;
}
// Driver Code
public static void main(String[] args)
{
int N = 2;
System.out.println(
countVowelPermutation(N));
}
}
Python3
# Python 3 program for the above approach
# Function to find the number of
# vowel permutations possible
def countVowelPermutation(n):
# To avoid the large output value
MOD = 1e9 + 7
# Initialize 2D dp array
dp = [[0 for i in range(5)] for j in range(n + 1)]
# Initialize dp[1][i] as 1 since
# string of length 1 will consist
# of only one vowel in the string
for i in range(5):
dp[1][i] = 1
# Directed graph using the
# adjacency matrix
relation = [[1],[0, 2], [0, 1, 3, 4], [2, 4],[0]]
# Iterate over the range [1, N]
for i in range(1, n, 1):
# Traverse the directed graph
for u in range(5):
dp[i + 1][u] = 0
# Traversing the list
for v in relation[u]:
# Update dp[i + 1][u]
dp[i + 1][u] += dp[i][v] % MOD
# Stores total count of permutations
ans = 0
for i in range(5):
ans = (ans + dp[n][i]) % MOD
# Return count of permutations
return int(ans)
# Driver code
if __name__ == '__main__':
N = 2
print(countVowelPermutation(N))
# This code is contributed by bgangwar59.
C#
// C# program to find absolute difference
// between the sum of all odd frequenct and
// even frequent elements in an array
using System;
using System.Collections.Generic;
class GFG {
// Function to find the number of
// vowel permutations possible
static int countVowelPermutation(int n)
{
// To avoid the large output value
int MOD = (int)(1e9 + 7);
// Initialize 2D dp array
long[,] dp = new long[n + 1, 5];
// Initialize dp[1][i] as 1 since
// string of length 1 will consist
// of only one vowel in the string
for (int i = 0; i < 5; i++) {
dp[1, i] = 1;
}
// Directed graph using the
// adjacency matrix
List> relation = new List>();
relation.Add(new List { 1 });
relation.Add(new List { 0, 2 });
relation.Add(new List { 0, 1, 3, 4 });
relation.Add(new List { 2, 4 });
relation.Add(new List { 0 });
// Iterate over the range [1, N]
for (int i = 1; i < n; i++)
{
// Traverse the directed graph
for (int u = 0; u < 5; u++)
{
dp[i + 1, u] = 0;
// Traversing the list
foreach(int v in relation[u])
{
// Update dp[i + 1][u]
dp[i + 1, u] += dp[i, v] % MOD;
}
}
}
// Stores total count of permutations
long ans = 0;
for (int i = 0; i < 5; i++)
{
ans = (ans + dp[n, i]) % MOD;
}
// Return count of permutations
return (int)ans;
}
// Driver code
static void Main() {
int N = 2;
Console.WriteLine(countVowelPermutation(N));
}
}
// This code is contributed by divyesh072019.
输出:
10
时间复杂度: O(N)
辅助空间: O(N)