【LeetCode 1220】 Count Vowels Permutation

题目描述

Given an integer n, your task is to count how many strings of length n can be formed under the following rules:

Each character is a lower case vowel (‘a’, ‘e’, ‘i’, ‘o’, ‘u’)
Each vowel ‘a’ may only be followed by an ‘e’.
Each vowel ‘e’ may only be followed by an ‘a’ or an ‘i’.
Each vowel ‘i’ may not be followed by another ‘i’.
Each vowel ‘o’ may only be followed by an ‘i’ or a ‘u’.
Each vowel ‘u’ may only be followed by an ‘a’.
Since the answer may be too large, return it modulo 10^9 + 7.

Example 1:

Input: n = 1
Output: 5
Explanation: All possible strings are: "a", "e", "i" , "o" and "u".

Example 2:

Input: n = 2
Output: 10
Explanation: All possible strings are: "ae", "ea", "ei", "ia", "ie", "io", "iu", "oi", "ou" and "ua".

Example 3:

Input: n = 5
Output: 68

Constraints:

1 <= n <= 2 * 10^4

思路

动态规划。dp[i][j]表示长度为i,以j为结尾,的串,个数。
根据限制条件可以推出每个字符前边的字符是什么,以当前字符结尾的个数为所有它前面可以出现的字符的个数之和。

代码

class Solution {
public:
    int countVowelPermutation(int n) {
        int MOD = 1e9+7;
        
        vector<vector<long long int> > dp(n+1, vector<long long>(5+1, 0));
        for (int i=1; i<=5; ++i) {
            dp[1][i] = 1;
        }
        
        for (int i=2; i<=n; ++i) {
            for (int j=1; j<=5; ++j) {
                long long tmp = 0;
                if (j == 1) {
                    tmp = (dp[i-1][2]%MOD + dp[i-1][3]%MOD + dp[i-1][5]%MOD)%MOD;
                }else if (j == 2) {
                    tmp = (dp[i-1][3]%MOD + dp[i-1][1]%MOD)%MOD;
                }else if (j == 3) {
                    tmp = (dp[i-1][4]%MOD + dp[i-1][2]%MOD)%MOD;
                }else if (j == 4) {
                    tmp = dp[i-1][3]%MOD;
                }else {
                    tmp = (dp[i-1][3]%MOD + dp[i-1][4]%MOD);
                }
                dp[i][j] += tmp;
                dp[i][j] %= MOD;
            }
        }
        
        long long res = 0;
        for (int i=1; i<=5; ++i) {
            res += dp[n][i];
            res %= MOD;
        }
        return res;
    }
};

最后不能直接求和然后取模,会超long long。
动态规划修行告一段落。甚至感觉比大学的时候理解的更深一点。
洗漱睡觉喽。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值