From e4a6a74e1837edc339d635b6ea555fce16d812ec Mon Sep 17 00:00:00 2001 From: Lam Haoyin Date: Mon, 17 Jan 2022 00:43:53 +0800 Subject: [PATCH] add: 220117 --- 2201/220117-CN.cpp | 59 +++++++++++++++++++++++++++++++++++++++++++++ 2201/CMakeLists.txt | 2 +- 2 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 2201/220117-CN.cpp diff --git a/2201/220117-CN.cpp b/2201/220117-CN.cpp new file mode 100644 index 0000000..1f1954c --- /dev/null +++ b/2201/220117-CN.cpp @@ -0,0 +1,59 @@ +#include + +/** + * 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. + */ + +class Solution { +private: + static const int mod = 1000000007; + // dp[currentLength][endsWith] + // int dp[20005][5]{{}, {1, 1, 1, 1, 1}}; + /** + * Rules of assembling characters: + * 'a' can be derived from those that ends with 'eiu'. + * 'e' <- 'ai' + * 'i' <- 'eo' + * 'o' <- 'i' + * 'u' <- 'io' + */ +public: + static int countVowelPermutation(int n) { + int dp[5]{1, 1, 1, 1, 1}, tmp[5]; + for (int i = 2; i <= n; ++i) { + // 'a' <- 'eiu' + tmp[0] = ((dp[1] + dp[2]) % mod + dp[4]) % mod; + + // 'u' <- 'io' + dp[4] = (dp[2] + dp[3]) % mod; + + // 'e' <- 'ai' + tmp[1] = (dp[0] + dp[2]) % mod; + + // 'o' <- 'i' + tmp[3] = dp[2]; + + // 'i' <- 'eo' + dp[2] = (dp[1] + dp[3]) % mod; + + dp[0] = tmp[0]; + dp[1] = tmp[1]; + dp[3] = tmp[3]; + } + return (((dp[1] + dp[2]) % mod + (dp[3] + dp[4]) % mod) % mod + dp[0]) % mod; + } +}; + +int main() { + Solution solver; + std::cout << solver.countVowelPermutation(20000); + return 0; +} \ No newline at end of file diff --git a/2201/CMakeLists.txt b/2201/CMakeLists.txt index aefcd37..89bead9 100644 --- a/2201/CMakeLists.txt +++ b/2201/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2201) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2201 220116.cpp) \ No newline at end of file +ADD_EXECUTABLE(2201 220117-CN.cpp) \ No newline at end of file