D. Easy Problem(简单DP)

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

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

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

 AC代码:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 2e5+10;
 4 # define inf 1ll<<60
 5 # define ll long long
 6 ll dp[5][maxn],cost[maxn];
 7 char f[5]= {"hard"};
 8 string str;
 9 ll n;
10 ll cal(int t1,int t2)
11 {
12     if(t1==4)
13         return inf;
14     if(t2==n)
15         return 0;
16     if(dp[t1][t2]!=-1)
17         return dp[t1][t2];
18     if( str[t2] == f[t1])
19     {
20         dp[t1][t2] =min( cal(t1+1,t2+1), cal(t1,t2+1)+cost[t2]);
21     }
22     else
23     {
24         dp[t1][t2] =cal(t1,t2+1);
25     }
26     return dp[t1][t2];
27 }
28 int main()
29 {
30     cin>>n;
31     cin>>str;
32     for(int i=0; i<n; i++)
33     {
34         cin>>cost[i];
35     }
36 //    for(int i=0;i<4;i++){
37 //    cout<<f[i]<<endl;
38 //    }
39     memset(dp,-1,sizeof(dp));
40     cout<<cal(0,0)<<endl;
41     return 0;
42 }
43  
原文地址:https://www.cnblogs.com/letlifestop/p/10262751.html