nico和niconiconi

原题地址:https://ac.nowcoder.com/acm/contest/3002/I

解题思路
一道中规中矩的动态规划,定义一个dp数组,储存每一个字母下的最大分数,最后输出dp[n]

AC代码

#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 300000 + 10;
char str[maxn];
typedef long long ll;
ll dp[maxn];

string substring(int a, int b)
{
    string res;
    for (int i = a; i <= b; i++)
    {
        res += str[i];
    }
    return res;
}

ll me_max(ll a,ll b)
{
    if(a>b) return a;
    else return b;
}

int main()
{//动态规划
    int n, a, b, c;
    scanf("%d%d%d%d%s", &n, &a, &b, &c, str + 1);
    dp[0] = 0;
    for (int i = 1; i <= n; i++)
    {
        dp[i] = dp[i - 1];
        if (i >= 4 && substring(i - 3, i) == "nico")
        {
            ll x = dp[i - 4] + a;
            dp[i] = me_max(x, dp[i]);
        }
        if (i >= 6 && substring(i - 5, i) == "niconi")
        {
            ll x = dp[i - 6] + b;
            dp[i] = me_max(x, dp[i]);
        }
        if (i >= 10 && substring(i - 9, i) == "niconiconi")
        {
            ll x = dp[i - 10] + c;
            dp[i] = me_max(x, dp[i]);
        }
    }
    cout<<dp[n];
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值