0
点赞
收藏
分享

微信扫一扫

力扣 1220.统计元首字母序列的数目(c语言)

雪域迷影 2022-01-17 阅读 26

1.题目描述

2.核心思路

一道dp问题,提示的很明显。但并不妨碍我看题解,实际上就是利用所给条件去写一个dp就行。

3.代码实现

typedef long long ll;//数据量大要用到long long 
int countVowelPermutation(int n){
    ll mod=1e9 + 7;
    ll ans=0;
    ll* dp=(ll*)malloc(sizeof(ll)*5);
    ll* ndp=(ll*)malloc(sizeof(ll)*5);
    for(int i=0;i<5;i++)
    {
        dp[i]=1;
    }
    for(int i=0;i<5;i++)
    {
        ndp[i]=0;
    }
    for(int i=2;i<=n;i++)
    {
        ndp[0]=(dp[1]+dp[4]+dp[2])%mod;
        ndp[1]=(dp[0]+dp[2])%mod;
        ndp[2]=(dp[1]+dp[3])%mod;
        ndp[3]=dp[2];
        ndp[4]=(dp[3]+dp[2])%mod;
        memcpy(dp,ndp,sizeof(ll)*5);
    }
    for(int i=0;i<5;i++)
    {
        ans=(ans+dp[i])%mod;
    }
    free(dp);
    free(ndp);
    return ans;  
}

4.总结

不看题解做不出来

举报

相关推荐

0 条评论