题意:
Vasya is preparing a contest, and now he has written a statement for an easy problem. The statement is a string of length ?n consisting of lowercase Latin latters. Vasya thinks that the statement can be considered hard if it contains a subsequence hard; otherwise the statement is easy. For example, hard, hzazrzd, haaaaard can be considered hard statements, while har, hart and drah are easy statements.
Vasya doesn’t want the statement to be hard. He may remove some characters from the statement in order to make it easy. But, of course, some parts of the statement can be crucial to understanding. Initially the ambiguity of the statement is 00, and removing ?i-th character increases the ambiguity by ??ai (the index of each character is considered as it was in the original statement, so, for example, if you delete character r from hard, and then character d, the index of d is still 44 even though you delete it from the string had).
Vasya wants to calculate the minimum ambiguity of the statement, if he removes some characters (possibly zero) so that the statement is easy. Help him to do it!
Recall that subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.
Input
The first line contains one integer ?n (1≤?≤1051≤n≤105) — the length of the statement.
The second line contains one string ?s of length ?n, consisting of lowercase Latin letters — the statement written by Vasya.
The third line contains ?n integers ?1,?2,…,??a1,a2,…,an (1≤??≤9982443531≤ai≤998244353).
Output
Print minimum possible ambiguity of the statement after Vasya deletes some (possibly zero) characters so the resulting statement is easy.
Examples
input
6
hhardh
3 2 9 11 7 1
output
5
input
8
hhzarwde
3 2 6 9 4 8 7 1
output
4
input
6
hhaarr
1 2 3 4 5 6
output
0
翻译成汉语,就是给你一个n个字符的字符串,每个字符都有一个权制,问你删除最小的权值,使得剩下的字符串里不包含子序列hard。问最小删除的字符的权值和是多少?
思路:
求最小删除的字符权值和,我们可以转化为求剩余不包含子序列hard的字符串的最大权值。我们可以设dp{i}{j}代表第i个字符为第j种状态的最大权值(状态共有5种,0代表0~i全部子序列都不包含h,1代表0~i全部子序列最多包含h,2代表0~i全部子序列最多包含ha,3代表0~i全部子序列最多包含har,4代表0~i全部子序列最多包含hard,最后dp{n}{0~3}的最大值即为答案)
代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
char s[100005];
long long dp[100005][5];
long long val[100005];
int main() {
int n;
memset(dp, 0, sizeof(dp));
scanf("%d", &n);
scanf("%s", s + 1);
long long sum = 0;
for(int i = 1; i <= n; i++) scanf("%lld", &val[i]), sum += val[i];
for(int i = 1; i <= n; i++) {
if (s[i] == 'h') {
dp[i][0] = dp[i - 1][0];
dp[i][1] = max(dp[i - 1][0], dp[i - 1][1]) + val[i];
for (int j = 2; j < 5; j++) {
dp[i][j] = dp[i - 1][j] + val[i];
}
} else if (s[i] == 'a') {
dp[i][0] = dp[i - 1][0] + val[i];
dp[i][1] = dp[i - 1][1];
dp[i][2] = max(dp[i - 1][1], dp[i - 1][2]) + val[i];
for (int j = 3; j < 5; j++) {
dp[i][j] = dp[i - 1][j] + val[i];
}
} else if (s[i] == 'r') {
dp[i][0] = dp[i - 1][0] + val[i];
dp[i][1] = dp[i - 1][1] + val[i];
dp[i][2] = dp[i - 1][2];
dp[i][3] = max(dp[i - 1][2], dp[i - 1][3]) + val[i];
for (int j = 4; j < 5; j++) {
dp[i][j] = dp[i - 1][j] + val[i];
}
} else if (s[i] == 'd') {
dp[i][0] = dp[i - 1][0] + val[i];
dp[i][1] = dp[i - 1][1] + val[i];
dp[i][2] = dp[i - 1][2] + val[i];
dp[i][3] = dp[i - 1][3];
dp[i][4] = max(dp[i - 1][3], dp[i - 1][4]) + val[i];
} else {
for (int j = 0; j < 5; j++) {
dp[i][j] = dp[i - 1][j] + val[i];
}
}
}
long long mx = 0;
for(int i = 0; i < 4; i++){
mx = max(mx, dp[n][i]);
}
printf("%lld\n", sum - mx);
return 0;
}
如果有写的不对或者不全面的地方 可通过主页的联系方式进行指正,谢谢