【Codeforces 1096D】Easy Problem
【链接】 我是链接,点我呀:)
【题意】
让你将一个字符串删掉一些字符。
使得字符串中不包含子序列"hard"
删掉每个字符的代价已知为ai
让你求出代价最小的方法.
【题解】
设dp[i][j]表示前i个字符,已经和"hard"匹配前j个的最小花费。
对于dp[i][j]
对s[i+1]分类讨论
①s[i+1]不删
那么有两种情况
第一种:s[i+1]和"hard"的第j+1个字符匹配
第二种:..xxxxx不匹配
则分别转移到dp[i+1][j+1]和dp[i+1][j]
②s[i+1]删掉
转移到dp[I+1][j]且用dp[i][j]+a[i+1]尝试转移。
【代码】
import java.io.*;
import java.util.*;
public class Main {
static InputReader in;
static PrintWriter out;
public static void main(String[] args) throws IOException{
//InputStream ins = new FileInputStream("E:\\rush.txt");
InputStream ins = System.in;
in = new InputReader(ins);
out = new PrintWriter(System.out);
//code start from here
new Task().solve(in, out);
out.close();
}
static int N = (int)1e5;
static class Task{
int n;
String s;
long a[] = new long[N+10];
String goal=new String(" hard");
long dp[][] = new long[N+10][10];
public void solve(InputReader in,PrintWriter out) {
n = in.nextInt();
s = in.next();
s = " "+s;
for (int i = 1;i <=n;i++) a[i] = in.nextLong();
for (int i = 0;i <= N;i++)
for (int j = 0;j <= 8;j++)
dp[i][j] = (long)(1e17);
dp[0][0] = 0;
for(int i = 0;i < n;i++)
for (int j = 0;j <= 3;j++) {
//第i+1个不删
if (s.charAt(i+1)==goal.charAt(j+1)) {
dp[i+1][j+1] = Math.min(dp[i+1][j+1], dp[i][j]);
}else {
dp[i+1][j] = Math.min(dp[i+1][j], dp[i][j]);
}
//第i+1个删掉
dp[i+1][j] = Math.min(dp[i+1][j], dp[i][j]+a[i+1]);
}
long ans = dp[n][0];
for (int i = 1;i <= 3;i++) {
ans = Math.min(ans, dp[n][i]);
}
out.println(ans);
}
}
static class InputReader{
public BufferedReader br;
public StringTokenizer tokenizer;
public InputReader(InputStream ins) {
br = new BufferedReader(new InputStreamReader(ins));
tokenizer = null;
}
public String next(){
while (tokenizer==null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(br.readLine());
}catch(IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
}
【Codeforces 1096D】Easy Problem的更多相关文章
- 【codeforces 749A】Bachgold Problem
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 527D】Clique Problem
[题目链接]:http://codeforces.com/contest/527/problem/D [题意] 一维线段上有n个点 每个点有坐标和权值两个域分别为xi,wi; 任意一对点(i,j) 如 ...
- 【codeforces 793C】Mice problem
[题目链接]:http://codeforces.com/contest/793/problem/C [题意] 给你每个点x轴移动速度,y轴移动速度; 问你有没有某个时刻,所有的点都"严格& ...
- 【codeforces 807D】Dynamic Problem Scoring
[题目链接]:http://codeforces.com/contest/807/problem/D [题意] 给出n个人的比赛信息; 5道题 每道题,或是没被解决->用-1表示; 或者给出解题 ...
- 【codeforces 239B】Easy Tape Programming
[题目链接]:http://codeforces.com/contest/239/problem/B [题意] 给你一个长度为n的字符串,只包括'<">'以及数字0到9; 给你q ...
- 【26.09%】【codeforces 579C】A Problem about Polyline
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 761D】Dasha and Very Difficult Problem
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 798C】Mike and gcd problem
[题目链接]:http://codeforces.com/contest/798/problem/C [题意] 给你n个数字; 要求你进行若干次操作; 每次操作对第i和第i+1个位置的数字进行; 将 ...
随机推荐
- mybatis批量update操作的写法,及批量update报错的问题解决方法
mybatis的批量update操作写法很简单,如下: public interface YourMapper extends BaseMapper<YourExt> { void upd ...
- [App Store Connect帮助]四、添加 App 图标、App 预览和屏幕快照(5)移除 App 预览或屏幕快照
您可以随时移除 App 预览,但仅可在 App 状态为可编辑时才能移除屏幕快照.要了解可编辑的状态,请前往 App 状态. 必要职能:“帐户持有人”职能.“管理”职能.“App 管理”职能或“营销”职 ...
- robotframework - 框架做接口自动化get请求
1.做get请求之前先安装 Request库,参考github上链接 :https://github.com/bulkan/robotframework-requests/#readme 2.请求&a ...
- 【转】Linux系统编程---dup和dup2详解
正常的文件描述符: 在linux下,通过open打开以文件后,会返回一个文件描述符,文件描述符会指向一个文件表,文件表中的节点指针会指向节点表.看下图: 打开文件的内核数据结构 dup和dup2两个函 ...
- HTML DOM getElementById() 方法
定义和用法 getElementById() 方法可返回对拥有指定 ID 的第一个对象的引用. 语法 document.getElementById(id) 说明 HTML DOM 定义了多种查找元素 ...
- Hbase源码分析:server端RPC
server端rpc包括master和RegionServer.接下来主要梳理一下,master和regionserver中有关rpc创建,启动以及处理的过程. 1,server rpc的初始化过程 ...
- sublime 自定义快捷键
[ { "keys": ["alt+space"], "command": "auto_complete" }, // ...
- Python学习日记之文件读取操作
Python内置了文件读写的函数open,read 用法示例: open('/home/root/files.txt ') 在打开文件后,操作完成后可以使用close()关闭文件,但比较好的文件读写方 ...
- android 蓝牙开发---与蓝牙模块进行通讯 基于eclipse项目
2017.10.20 之前参加一个大三学长的创业项目,做一个智能的车锁App,用到嵌入式等技术,App需要蓝牙.实时位置等技术,故查了几篇相关技术文章,以此参考! //先说 ...
- IT项目为什么失败 --美国IT项目管理硕士笔记(一)
IT项目为什么失败 什么是项目 项目可以被看作任何一系列的活动和任务.这些活动和任务有一个特定目标需要在特定要求下完成,并有一个明确的开始结束日期和资金限制(如果有).项目需要消耗人力或非人力资源 ...