【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个位置的数字进行; 将 ...
随机推荐
- jquery input 赋值和取值
记录一下: 在写一个input赋值,二话不说就直接利用了$('#xx').val()来进行取值和赋值,取值ok,赋值后利用alert显示正确,但是在html上并没有正确的显示出来,后来改为使用如下代码 ...
- wamp集成环境下帝国备份出错
我在本地wamp环境下面使用帝国备份王时,报错信息如下: Parse error: syntaxerror, unexpected $end in D:wampwwwhuifuclassfunctio ...
- FFT学习及简单应用(一点点详细)
什么是FFT 既然打开了这篇博客,大家肯定都已经对FFT(Fast Fourier Transformation)有一点点了解了吧 FFT即为快速傅里叶变换,可以快速求卷积(当然不止这一些应用,但是我 ...
- 1、DOS基本命令
命令dir能给列出当前目录下面的所有文件.程序和子目录.所有目录(Windows 中称为文件夹)的目录名前面都有一个<DIR>标记.文件和程序名前面显示有这些文件和程序的大小. 想说的是, ...
- 无法连接到已配置的web服务器
问题:如题,asp.net WebForm的项目,在vs中选择aspx文件在浏览器中查看时候回报该错误. 网上最多的解决方案是防火墙的原因,说关闭防火墙就可以了.但问题是我另一个项目没有问题啊,所以不 ...
- Spark学习之键值对(pair RDD)操作(3)
Spark学习之键值对(pair RDD)操作(3) 1. 我们通常从一个RDD中提取某些字段(如代表事件时间.用户ID或者其他标识符的字段),并使用这些字段为pair RDD操作中的键. 2. 创建 ...
- Java&Xml教程(八)使用JDOM将Java对象转换为XML
在前面的教程中我们学习了如何使用JDOM解析和修改XML文件内容,本节介绍如何将Java对象转换为XML数据并生成文件. JDOM的Document类提供了便捷的方法创建元素和属性,XMLOutput ...
- 关于微信小程序:scroll-view,backgroundTextStyle
踩过的坑mark下 1.滚动列表最好不要用scroll-view组件,这个组件有不少问题,比如触顶操作触发了,会连续好几次执行触发函数,得用一个开关变量和定时器配合来控制,一般情况下view组件就够用 ...
- python自动化--语言基础三字典、函数、全局/局部变量
字典 dict1 = {,'class':'first'} print(dict1.keys()) #打印所有的key值 print(dict1.values()) #打印所有的values值 pri ...
- iOS从手机相册选择一张照片并显示 Objective-C
要先给app设置访问相册的权限: 在项目的Info.plist文件里添加Privacy - Photo Library Usage Description权限 ViewController.h: #i ...