【leetcode_1220】【困难】count-vowels-permutation / 统计元音字母序列的数目
生活随笔
收集整理的這篇文章主要介紹了
【leetcode_1220】【困难】count-vowels-permutation / 统计元音字母序列的数目
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- URL
- 題目
- 分析
- 源碼
- DP動態規劃
- 矩陣快速冪
- 源碼概述
- 小結
URL
鏈接:https://leetcode-cn.com/problems/count-vowels-permutation/
題目
分析
源碼
DP動態規劃
#include <stdio.h> #include <stdlib.h> #include <string.h>typedef long long LL;int countVowelPermutation(int n){/* 0.1 */long long mod = 1e9 + 7 ; //取模 避免數據過大long long ans = 0; //返回值 存儲最后所有的可能項的數量LL * dp = (LL *)malloc(sizeof(LL)*5); // 前一次計算數據LL * ndp = (LL *)malloc(sizeof(LL)*5); // 存儲當前次計算數據/* a , e , i , o , u *//* 0 , 1 , 2 , 3 , 4 *//* 1.1 *//* n = 1 */for (int i = 0;i < 5 ; i++){dp[i] = 1;}/* 1.2 *//* n >= 2 */for (int i=2;i<=n;i++){ // Tip : i <= n/* 1.3 *//* e , u , i + a */ndp[0] = (dp[1] + dp[2] + dp[4]) % mod; // n >= 2時,以各個元音結尾的數量(n為i的時候的數據)/* a , i + e */ndp[1] = (dp[0] + dp[2]) % mod;/* e , o + i */ndp[2] = (dp[1] + dp[3]) % mod;/* i + o *///ndp[3] = (dp[2]) % mod;ndp[3] = dp[2]; //Tip : it is already been calculated by mod/* i , o + u */ndp[4] = (dp[2] + dp[3]) % mod;/* 1.4 */memcpy(dp , ndp , sizeof(LL)*5 ); // 將當前次各個數量賦值給下一次待計算的數值變量}/* 2.1 */for (int i =0 ;i < 5 ;i++){ans = ( ans + dp[i]) % mod;}/* 0.2 */free(dp);free(ndp);return ans; }int main() {int n = 0;int ans = 0;n = 1; // -> 5ans = countVowelPermutation(n);printf("n = %d , len = %d\n",n,ans);n = 2; // -> 10ans = countVowelPermutation(n);printf("n = %d , len = %d\n",n,ans);n = 5; // -> 68ans = countVowelPermutation(n);printf("n = %d , len = %d\n",n,ans);return 0; }矩陣快速冪
略源碼概述
dp動態規劃
矩陣快速冪
小結
dp動態規劃
矩陣快速冪
總結
以上是生活随笔為你收集整理的【leetcode_1220】【困难】count-vowels-permutation / 统计元音字母序列的数目的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安卓随机通话记录_Android 通话记
- 下一篇: 聊聊人像抠图背后的算法技术