📌  相关文章
📜  计算可以根据给定条件生成的所有可能的 N 长度元音排列

📅  最后修改于: 2021-10-25 04:39:55             🧑  作者: Mango

给定一个整数N ,任务是计算由小写元音组成的 N 长度字符串的数量,这些字符串可以根据以下条件生成:

  • 每个‘a’后面只能跟一个‘e’
  • 每个“e”后面只能跟一个“a”“i”。
  • 每个‘i’后面不能跟另一个‘i’
  • 每个‘o’后面只能跟一个‘i’或一个‘u’
  • 每个‘u’后面只能跟一个‘a’

例子:

方法:解决这个问题的想法是将其可视化为图形问题。根据给定的规则,可以构建有向图,其中从uv的边意味着v可以立即写在结果字符串u之后。问题简化为在构造的有向图中找到 N 长路径的数量。请按照以下步骤解决问题:

  • 让元音a, e, i, o, u分别编号为0, 1, 2, 3, 4 ,并使用给定图中所示的依赖关系,将图转换为邻接表关系,其中索引表示元音并且该索引处的列表表示从该索引到列表中给出的字符的边。

  • 初始化一个二维数组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.


Javascript


输出:
10

时间复杂度: O(N)
辅助空间: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程