Educational Codeforces Round 57 (Rated for Div. 2) D dp
https://codeforces.com/contest/1096/problem/D
题意
给一个串s,删掉一个字符的代价为a[i],问使得s的子串不含"hard"的最小代价
题解
- 定义\(dp[i][j]\)为到第i位下一个将要匹配j的最小代价
- \(若s[i]==t[j]\)
- 删掉:\(min(dp[i+1][j],dp[i][j]+a[i])\)
- 不删,若j<3:\(min(dp[i+1][j+1],dp[i][j])\)
- \(若s[i]!=t[j]\)
- 不用删:\(min(dp[i+1][j],dp[i][j])\)
- \(若s[i]==t[j]\)
- \(ans=min(dp[n][i]),0 \leq i \leq 3\)
代码
#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define MAXN 100005
using namespace std;
ll dp[MAXN][4],a[MAXN],ans;
int n;string s;
int main(){
cin>>n>>s;
memset(dp,inf,sizeof(dp));
for(int i=0;i<n;i++)scanf("%lld",&a[i]);
string t="hard";
dp[0][0]=0;
for(int i=0;i<n;i++){
for(int j=0;j<4;j++){
if(dp[i][j]==inf)continue;
if(s[i]==t[j]){
dp[i+1][j]=min(dp[i+1][j],dp[i][j]+a[i]);
if(j<3)dp[i+1][j+1]=min(dp[i+1][j+1],dp[i][j]);
}else{
dp[i+1][j]=min(dp[i+1][j],dp[i][j]);
}
}
}
ans=1e18;
for(int i=0;i<4;i++)ans=min(ans,dp[n][i]);
cout<<ans;
}
Educational Codeforces Round 57 (Rated for Div. 2) D dp的更多相关文章
- Educational Codeforces Round 57 (Rated for Div. 2) ABCDEF题解
题目总链接:https://codeforces.com/contest/1096 A. Find Divisible 题意: 给出l,r,在[l,r]里面找两个数x,y,使得y%x==0,保证有解. ...
- Educational Codeforces Round 57 (Rated for Div. 2) C 正多边形 + 枚举
https://codeforces.com/contest/1096/problem/C 题意 问是否存在一正多边形内三点构成的角度数为ang,若存在输出最小边数 题解 三点构成的角是个圆周角,假设 ...
- Educational Codeforces Round 57 (Rated for Div. 2)
我好菜啊. A - Find Divisible 好像没什么可说的. #include<cstdio> #include<cstring> #include<algori ...
- Educational Codeforces Round 57 (Rated for Div. 2)D(动态规划)
#include<bits/stdc++.h>using namespace std;char s[100007];long long a[100007];long long dp[100 ...
- Educational Codeforces Round 57 (Rated for Div. 2) 前三个题补题
感慨 最终就做出来一个题,第二题差一点公式想错了,又是一波掉分,不过我相信我一定能爬上去的 A Find Divisible(思维) 上来就T了,后来直接想到了题解的O(1)解法,直接输出左边界和左边 ...
- Educational Codeforces Round 58 (Rated for Div. 2) F dp + 优化(新坑) + 离线处理
https://codeforces.com/contest/1101/problem/F 题意 有n个城市,m辆卡车,每辆卡车有起点\(s_i\),终点\(f_i\),每公里油耗\(c_i\),可加 ...
- Educational Codeforces Round 63 (Rated for Div. 2) D dp(最大连续子序列)
https://codeforces.com/contest/1155/problem/D 题意 一个n个数的数组\(a[i]\),可以选择连续的一段乘x,求最大连续子序列的值 题解 错误思路:贪心, ...
- Educational Codeforces Round 76 (Rated for Div. 2)E(dp||贪心||题解写法)
题:https://codeforces.com/contest/1257/problem/E 题意:给定3个数组,可行操作:每个数都可以跳到另外俩个数组中去,实行多步操作后使三个数组拼接起来形成升序 ...
- Educational Codeforces Round 71 (Rated for Div. 2)-E. XOR Guessing-交互题
Educational Codeforces Round 71 (Rated for Div. 2)-E. XOR Guessing-交互题 [Problem Description] 总共两次询 ...
随机推荐
- 小米笔记本pro 黑苹果系统无法进入系统,频繁重启故障解决记录
问题1:频繁重启,然后clover丢失 表现情况:开机没有选择macos 或windos的界面 解决办法:进入windows使用工具easyefi,直接添加一个clover start boot,选择 ...
- Element-ui 2.8.0版本中提升表格性能,做了哪些事情,原理是什么
背景 项目中一直用element-ui,之前用el-table的时候,发现表格数据较多时,滑动表格就会很卡.我们的表格中只有200行数据,每行大概有30的字段,表格滑动就卡的不行.在Element-u ...
- 1+x证书Web前端开发中级理论考试(试卷1)
2019年下半年 Web前端开发中级 理论考试 (考试时间19:00-20:30 共150分钟,测试卷1) 本试卷共3道大题,满分100分. 请在指定位置作答. 一.单选题(每小题2分,共30小题,共 ...
- spring tomcat启动 请求处理
onRefresh(); protected void onRefresh() { try { createEmbeddedServletContainer(); } } private void c ...
- windowsServer ------ 安装IIS
1.找到服务器管理器,点击添加角色,一步步执行 2.添加IIS 相关组件 勾选web服务器 下一步 将web服务iis 相关组件全部勾选,ftp 可不选 选择好后安装 等一会 关闭 可以查看到所安装角 ...
- python基础之Matplotlib库的使用一(平面图)
在我们过去的几篇博客中,说到了Numpy的使用,我们可以生成一些数据了,下面我们来看看怎么让这些数据呈现在图画上,让我们更加直观的来分析数据. 安装过程我就不再说了,不会安装的,回去补补python最 ...
- linq 获取不重复数据,重复数据 var unique = arr.GroupBy(o => o).Where(g => g.Count() == 1) .Select(g => g.ElementAt(0));
static void Main(string[] args) { int[] arr = { 1, 3, 3, 3, 3, 4, 5, 4, 5, 8, 9, 3 }; //不重复 var uniq ...
- LeetCode 905. Sort Array By Parity 按奇偶校验排列数组
题目 Given an array A of non-negative integers, return an array consisting of all the even elements of ...
- vue打包后刷新页面报错:Unexpected token <
前言 今天遇到了一个很怪的问题,在vue-cli+webpack的项目中,刷新特定页面后页面会变空白,报错为index.html文件中Unexpected token <. 怪点一是开发环境没有 ...
- maven 学习---Maven依赖机制
在 Maven 依赖机制的帮助下自动下载所有必需的依赖库,并保持版本升级. 案例分析 让我们看一个案例研究,以了解它是如何工作的.假设你想使用 Log4j 作为项目的日志.这里你要做什么? 1.在传统 ...