Easy Problem(动态规划)

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

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

The third line contains $ n n n$ integers $ a 1 , a 2 , … , a n a_1, a_2, \dots, a_n a1,a2,,an$ ($ 1 ≤ a i ≤ 998244353 1 \le a_i \le 998244353 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 ardh.

In the second example, $ 5 5 5$-th character is removed so the result is hhzawde.

In the third example there’s no need to remove anything.
题意很简单,就是删除字符,使之没有hard子序列。问最小的花费是多少。
这种题目就是看删除前面字符对于后面的影响。dp[0~3]分别代表着删除h到d的最小花费。举个例子,就像hhard这种的,遇见a的时候,我们会想是单纯的删除a价值小,还是删除两个h价值小。这样就有了递推公式:dp[i]=min(dp[i-1],dp[i]+a[i])。
代码如下:

#include<bits/stdc++.h>
#define ll long long
#define inf 0x7f7f7f7f
using namespace std;

const int maxx=1e6+100;
ll dp[10];
ll a[maxx];
string s;
int n;

int main()
{
	while(~scanf("%d",&n))
	{
		cin>>s;
		for(int i=0;i<n;i++) scanf("%lld",&a[i]);
		memset(dp,0,sizeof(dp)); 
		for(int i=0;i<n;i++)
		{
			if(s[i]=='h') dp[0]+=a[i];
			else if(s[i]=='a') dp[1]=min(dp[0],dp[1]+a[i]);
			else if(s[i]=='r') dp[2]=min(dp[1],dp[2]+a[i]);
			else if(s[i]=='d') dp[3]=min(dp[2],dp[3]+a[i]); 
		}
		printf("%lld\n",dp[3]);
	}
}

dp一定多想想,有时候没有看起来那么难!
努力加油a啊,(o)/~

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
根据引用,当在PHP执行Curl时遇到"CURL ERROR: Recv failure: Connection reset by peer"的错误提示,可以参考以下解决方法。首先,可以尝试通过更改Curl的配置来解决该问题。可以将CURLOPT_TCP_KEEPALIVE设置为true,将CURLOPT_TCP_KEEPIDLE设置为120,将CURLOPT_TCP_KEEPINTVL设置为60。这些设置将保持与服务器的连接,并防止连接由于长时间闲置而被重置。 另外,可以通过调整系统的防火墙设置,以确保Curl请求能够正常连接到目标服务器。可以尝试关闭或禁用防火墙,或者添加相应的规则以允许Curl请求通过。 根据引用,如果是在使用easywechat时遇到curl error 1014,可以根据以下方法进行解决。首先,需要在配置文件中添加相关代码,其中包括'guzzle'选项部分。在该部分,需要设置'verify'为false以忽略SSL证书的验证,并设置'timeout'为适当的超时时间,比如4.0秒。 然后,根据配置信息创建一个easywechat应用程序对象,并使用该对象进行相应的操作,比如处理红包相关功能。 综上所述,可以通过调整Curl的配置以及easywechat的相关设置来解决curl error 1014的问题。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [PHP执行Curl时报错提示CURL ERROR: Recv failure: Connection reset by peer的解决方法](https://download.csdn.net/download/weixin_38516491/13045559)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [解决EasyWeChat报错cURL error 60: SSL certificate problem: unable to get local issuer certificate](https://blog.csdn.net/robin_sky/article/details/90897154)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值