D. Easy Problem(简单DP)

题目链接:http://codeforces.com/contest/1096/problem/D

题目大意:给你一个字符串,然后再给你去掉每个字符串的每个字符的花费,然后问你使得字符中不再存在hard这个单词,可以是不连续的。

 具体思路:我们从头开始,非hard的单词就不需要考虑了,然后考虑一下,当遇到a的时候,我们就考虑构成h的最小花费,当遇到har的时候,我们就考虑构成ha的最小花费,当遇到hard的时候,我们就考虑构成hard的最小花费就可以了。

 AC代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5+10;
# define inf 1ll<<60
# define ll long long
ll dp[5][maxn],cost[maxn];
char f[5]= {"hard"};
string str;
ll n;
ll cal(int t1,int t2)
{
    if(t1==4)
        return inf;
    if(t2==n)
        return 0;
    if(dp[t1][t2]!=-1)
        return dp[t1][t2];
    if( str[t2] == f[t1])
    {
        dp[t1][t2] =min( cal(t1+1,t2+1), cal(t1,t2+1)+cost[t2]);
    }
    else
    {
        dp[t1][t2] =cal(t1,t2+1);
    }
    return dp[t1][t2];
}
int main()
{
    cin>>n;
    cin>>str;
    for(int i=0; i<n; i++)
    {
        cin>>cost[i];
    }
//    for(int i=0;i<4;i++){
//    cout<<f[i]<<endl;
//    }
    memset(dp,-1,sizeof(dp));
    cout<<cal(0,0)<<endl;
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值