题目链接: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;
}