「CF1096D」Easy Problem【dp】

D. Easy Problem

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output

Vasya is preparing a contest, and now he has written a statement for an easy problem. The statement is a string of length ? 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 0, and removing ?-th character increases the ambiguity by ?? (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 4 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 ≤ n ≤ 1 0 5 ) n\ (1≤n≤10^5) n (1n105) — the length of the statement.

The second line contains one string ? of length ?, consisting of lowercase Latin letters — the statement written by Vasya.

The third line contains ? integers a 1 , a 2 , … , a n ( 1 ≤ a i ≤ 998244353 ) . a_1,a_2,…,a_n (1≤a_i≤998244353). a1,a2,,an(1ai998244353).

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

Note

In the first example, first two characters are removed so the result is a r d h ardh ardh.
In the second example, 5 − t h 5-th 5th character is removed so the result is h h z a w d e hhzawde hhzawde.
In the third example there’s no need to remove anything.

  • 题意:

    • 给你一个字符串 s s s,每个位置的字符都有一个删除的代价,现在可以删除一定量的字符使得新字符串不包含子串"hard"(可以不连续),求最小代价
  • 解法 ⇒ \Rightarrow d p dp dp

    • 首先明确目标状态:前n个字符不包含长度为4的“hard”子串,那么可以用 d p [ i ] [ j ] dp[i][j] dp[i][j]表示前 i i i个字符形成的串不包含子串"hard"的前 j j j位的最小代价
    • 然后考虑状态转移:首先定义 t t t=“hard”,第 i i i位要么删掉,要么不删
      • 如果 s [ i ] ! = t [ j − 1 ] s[i]!=t[j-1] s[i]!=t[j1],也就是说当前位不可能对构成"hard"前 j j j位构成贡献,从贪心的角度,当前这个字符可以不删,所以 d p [ i ] [ j ] = d p [ i − 1 ] [ j ] dp[i][j]=dp[i-1][j] dp[i][j]=dp[i1][j]
      • 否则,当前这个字符要么删掉,要么不删掉:
        • 删掉:要是前 i i i位不包含"hard"前 j j j位的子串,而当前位删掉了,那么只有前 i − 1 i-1 i1位了,那么前 i − 1 i-1 i1位一定不能包含"hard"的前 j j j位,代价也就是 d p [ i − 1 ] [ j ] dp[i-1][j] dp[i1][j],所以这时候的最小代价就是 d p [ i − 1 ] [ j ] + a [ i ] dp[i-1][j]+a[i] dp[i1][j]+a[i]
        • 否则:同理,前 i − 1 i-1 i1位一定不能包含“hard” 的前 j − 1 j-1 j1位子串,所以最小代价就是 d p [ i − 1 ] [ j − 1 ] dp[i-1][j-1] dp[i1][j1]
  • 综上所述,转移方程如下:
    d p [ i ] [ j ] = { d p [ i − 1 ] [ j ] s [ i ] ! = t [ j − 1 ] m i n ( d p [ i − 1 ] [ j − 1 ] , d p [ i − 1 ] [ j ] + a [ i ] ) e l s e dp[i][j]=\left\{ \begin{array}{rcl} dp[i-1][j] & & {s[i]!=t[j-1]}\\ min(dp[i-1][j-1],dp[i-1][j]+a[i]) & & {else}\\ \end{array} \right. dp[i][j]={dp[i1][j]min(dp[i1][j1],dp[i1][j]+a[i])s[i]!=t[j1]else

  • 代码

#include

using namespace std;
typedef long long ll;
const int maxn=100005;

int n,a[maxn];ll dp[100005][10];
char s[100005];
char t[]="hard";

int main()
{
	scanf("%d",&n);
	scanf("%s",s+1);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	memset(dp,0x3f,sizeof(dp));
	for(int i=1;i<=4;i++) dp[0][i]=0;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=4;j++){
			if(s[i]!=t[j-1]) dp[i][j]=dp[i-1][j];
			else dp[i][j]=min(dp[i-1][j-1],dp[i-1][j]+1LL*a[i]);
		}
	}
	printf("%lld\n",dp[n][4]);
}

你可能感兴趣的:(动态规划)