题意:字符串每个字符带有一个权值,求删除字符,使字符串中不含有“hard”子串的最小成本。
题解:字符串dp
用dp[i] (i = 1, 2, 3 ,4)来表示遇到某个字符时,删或不删的最小值。
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<fstream>
#include<set>
#include<map>
#define ll long long
using namespace std;
const int maxn = 1e5 + 10;
int n, a;
ll dp[5];
char s[maxn];
int main() {
scanf("%d%s", &n, s + 1);
for (int i = 1; i <= n; i++) {
scanf("%d", &a);
if (s[i] == 'h') dp[1] += a;
else if (s[i] == 'a') dp[2] = min(dp[1], dp[2] + a);
else if (s[i] == 'r') dp[3] = min(dp[2], dp[3] + a);
else if (s[i] == 'd') dp[4] = min(dp[3], dp[4] + a);
}
printf("%lld\n", dp[4]);
return 0;
}