codeforces 1096 D. Easy Problem(dp)

题目:http://codeforces.com/contest/1096/problem/D

题意:给你一个字符串,每个字符有权值,问要删除的最小权值使得这个字符串中没有hard

思路:可以看作一个动态规划问题,dp代表无法构成前缀字符的代价,dp[1]对应h前缀字符的代价,dp[2]对应ha前缀字符的代价,dp[3]对应har的代价,dp[4]对应hard代价。
转移方程就相当于我们每次min(不保留该字母,不保留前缀)答案就是最后不保留hard 的值

#include<bits/stdc++.h>
#define fi first
#define se second
#define FOR(a) for(int i=0;i<a;i++)
#define sc(a) scanf("%d",&a)
#define show(a) cout<<a<<endl;
#define show2(a,b) cout<<a<<" "<<b<<endl;
#define show3(a,b,c) cout<<a<<" "<<b<<" "<<c<<endl;
using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<P, int> LP;
const ll inf = 1e17 + 10;
const int N = 1e6 + 10;
const ll mod = 998244353;

map<string, int>ml;



ll b[N], vis[N], po[N], num[N], t, n, m, x, y, k, f[N],a[N];
ll ex, ey, cnt, ans, sum, flag;
ll dist[N];
ll dp[N];
string s ;

vector<int> v[N];
map<int, int> mp;

int main()
{
	ios::sync_with_stdio ( false );
	cin.tie ( 0 );

	cin>>n;
	cin>>s;
	for(int i=0;i<n;i++) cin>>a[i];
	for(int i=0;s[i];i++)
	{
		if(s[i]=='h') num[1]+=a[i];
		if(s[i]=='a') num[2]=min(num[1],num[2]+a[i]);
		if(s[i]=='r') num[3]=min(num[2],num[3]+a[i]);
		if(s[i]=='d') num[4]=min(num[3],num[4]+a[i]);
	}
	cout<<num[4];





}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值