Codeforces 1096D Easy Problem 【DP】
<题目链接>
题目大意:
给你一个字符串,每个字符有权值,问现在删除字符串中的字符使其中没有"hard"的最小代价是多少。
解题分析:
用DP来求解: 转载于 >>>
dp[i][1]:表示字符串s的前i个字符中不含有前缀'h'的最小代价
dp[i][2]:表示字符串s的前i个字符中不含有前缀'ha'的最小代价
dp[i][3]:表示字符串s的前i个字符中不含有前缀'har'的最小代价
dp[i][4]:表示字符串s的前i个字符中不含有前缀'hard'的最小代价
对于状态转移,例如对dp[i][3],如果位置i的字符不是r,那么dp[i][3] = dp[i - 1][3];
否则,要么去掉位置i的字符,则代价为dp[i - 1][3] + a[i],如果不去除该位置字符,那么之前的序列不能含有'ha',则代价为dp[i - 1][2]。
#include <bits/stdc++.h>
using namespace std; #define N int(1e5+7)
typedef long long ll;
ll dp[N][],arr[N];
char s[N];
const ll INF = 1e18;
const char str[]="0hard"; int main(){
int n;scanf("%d",&n);
scanf("%s",s+);
for(int i=;i<=n;i++)scanf("%d",&arr[i]);
for(int i=;i<=n;i++)dp[i][]=INF;
if(s[]=='h')dp[][]=arr[];
for(int i=;i<=n;i++)
for(int j=;j<=;j++){
if(s[i]!=str[j])dp[i][j]=dp[i-][j];
else dp[i][j]=min(dp[i-][j]+arr[i],dp[i-][j-]); //以j=3为例,dp[i-1][j]为前i-1项不含'har',并且去除第j项的'r'的代价(因为前i-1项可能包含'ha'),dp[i-1][j-1]表示前i-1项连'ha'都不包含
}
printf("%lld\n",dp[n][]);
}
一维的写法:
#include <bits/stdc++.h>
using namespace std; typedef long long ll;
ll dp[];
char s[int(1e5+)];
const char ss[]="0hard";
int main(){
int n;cin>>n;
scanf("%s",s+);
for(int i=;i<=n;i++){
ll now;scanf("%lld",&now);
if(s[i]=='h')dp[]+=now;
for(int j=;j<=;j++)
if(s[i]==ss[j])dp[j]=min(dp[j]+now,dp[j-]);
}
printf("%lld\n",dp[]);
}
2019-02-17
Codeforces 1096D Easy Problem 【DP】的更多相关文章
- Codeforces 1096D - Easy Problem - [DP]
题目链接:http://codeforces.com/problemset/problem/1096/D 题意: 给出一个小写字母组成的字符串,如果该字符串的某个子序列为 $hard$,就代表这个字符 ...
- poj2826 An Easy Problem?!【计算几何】
含[三点坐标计算面积].[判断两线段是否有交点].[求线段交点]模板 An Easy Problem?! Time Limit: 1000MS Memory Limit: 65536K Tot ...
- Codeforces 1027E Inverse Coloring 【DP】
Codeforces 1027E Inverse Coloring 题目链接 #include<bits/stdc++.h> using namespace std; #define N ...
- [CodeForces - 1225E]Rock Is Push 【dp】【前缀和】
[CodeForces - 1225E]Rock Is Push [dp][前缀和] 标签:题解 codeforces题解 dp 前缀和 题目描述 Time limit 2000 ms Memory ...
- HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】
HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...
- HDOJ 1501 Zipper 【DP】【DFS+剪枝】
HDOJ 1501 Zipper [DP][DFS+剪枝] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- HDOJ 1257 最少拦截系统 【DP】
HDOJ 1257 最少拦截系统 [DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDOJ 1159 Common Subsequence【DP】
HDOJ 1159 Common Subsequence[DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- HDOJ_1087_Super Jumping! Jumping! Jumping! 【DP】
HDOJ_1087_Super Jumping! Jumping! Jumping! [DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
随机推荐
- Swift 学习- 08 -- 闭包
// 闭包是自包含的函数代码块, 可以在代码中被传递和使用, swift 中的闭包 与 C语言 和 OC 中的代码块 (blocks) 以及其他一些编程语言中的匿名函数比较类似 // 闭包可以捕获和存 ...
- git使用中出现的错误
因同时有两个git账户,之前登录了git A 用户在使用了 1. 长期存储密码 git config --global credential.helper store 之后在git B 账 ...
- Netty沾包和拆包
1.连着发两条,会沾在一起,这就是沾包 2.包尾添加特殊分隔符,接收方通过特殊分隔符切分报文区分,这就是拆包 在ChatServerInit类.ChatClientInit类分别加入以下代码 Byte ...
- NumPy:数组计算
一.MumPy:数组计算 1.NumPy是高性能科学计算和数据分析的基础包.它是pandas等其他各种工具的基础.2.NumPy的主要功能: ndarray,一个多维数组结构,高效且节省空间 无需循环 ...
- PHP Warning: mysqli_connect(): The server requested authentication method unknown to the client [caching_sha2_password] in /usr/local/php/CreateDB.php on line 5
原因:php还不支持mysql8.0最新的密码加密方式 ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY ' ...
- poj2417 bsgs算法非逆元模板,用于求解A^x=B(mod C)的方程
参考博客 https://blog.csdn.net/clover_hxy/article/details/50683832关于欧拉定理推论的证明 https://www.cnblogs.com/as ...
- git bash中的快捷键
转载: https://www.cnblogs.com/dhuhewang/p/6504914.html 1.bash命令格式 命令 [-options] [参数],如:tar zxvf dem ...
- AI-序列化-做五个数据接口
#url.py url(r'^customer/$', views.CustomerView.as_view()), #查询所有数据.添加数据接口url url(r'^customer/(\d+)', ...
- C/C++中二进制与文本方式打开文件的区别
二进制与文本文件主要有两个大的区别: 1.换行符的区别: Windows平台下 对于Windows文本文件,它们使用回车和换行来表示换行符:如果以“文本”方式打开文件,当读取文件的时候,系统会将所有 ...
- 论文阅读笔记三十九:Accurate Single Stage Detector Using Recurrent Rolling Convolution(RRC CVPR2017)
论文源址:https://arxiv.org/abs/1704.05776 开源代码:https://github.com/xiaohaoChen/rrc_detection 摘要 大多数目标检测及定 ...