【链接】 我是链接,点我呀:)

【题意】

让你将一个字符串删掉一些字符。
使得字符串中不包含子序列"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的更多相关文章

  1. 【codeforces 749A】Bachgold Problem

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  2. 【codeforces 527D】Clique Problem

    [题目链接]:http://codeforces.com/contest/527/problem/D [题意] 一维线段上有n个点 每个点有坐标和权值两个域分别为xi,wi; 任意一对点(i,j) 如 ...

  3. 【codeforces 793C】Mice problem

    [题目链接]:http://codeforces.com/contest/793/problem/C [题意] 给你每个点x轴移动速度,y轴移动速度; 问你有没有某个时刻,所有的点都"严格& ...

  4. 【codeforces 807D】Dynamic Problem Scoring

    [题目链接]:http://codeforces.com/contest/807/problem/D [题意] 给出n个人的比赛信息; 5道题 每道题,或是没被解决->用-1表示; 或者给出解题 ...

  5. 【codeforces 239B】Easy Tape Programming

    [题目链接]:http://codeforces.com/contest/239/problem/B [题意] 给你一个长度为n的字符串,只包括'<">'以及数字0到9; 给你q ...

  6. 【26.09%】【codeforces 579C】A Problem about Polyline

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  7. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  8. 【codeforces 761D】Dasha and Very Difficult Problem

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. 【codeforces 798C】Mike and gcd problem

    [题目链接]:http://codeforces.com/contest/798/problem/C [题意] 给你n个数字; 要求你进行若干次操作; 每次操作对第i和第i+1个位置的数字进行; 将 ...

随机推荐

  1. bzoj 1833: [ZJOI2010]count 数字计数【数位dp】

    非典型数位dp 先预处理出f[i][j][k]表示从后往前第i位为j时k的个数,然后把答案转换为ans(r)-ans(l-1),用预处理出的f数组dp出f即可(可能也不是dp吧--) #include ...

  2. bzoj 1679: [Usaco2005 Jan]Moo Volume 牛的呼声【枚举】

    直接枚举两两牛之间的距离即可 #include<iostream> #include<cstdio> #include<algorithm> using names ...

  3. 使用particles.js实现网页背景粒子特效

    得知途径 B3log提供了两套博客系统,一个是用Java开发的,叫做Solo,我也是在网上搜索Java博客系统时发现了它,之后才了解了B3log:还有一个是用Go语言开发的,叫做Pipe.其中Solo ...

  4. VBNET AUTOCAD NETAPI 让插件随autocad启动

    定义一个函数,随AutoCAD 启动加载当前程序集到autocad,涉及到写入注册表,注意这是在autocad内部加载dll之后处理的方法.... 写入HKLM表示所有登录的用户都会受影响(autoc ...

  5. 使用 typescript 和 canvas 重构snow效果

    前言:之前做过一个 snow 效果,但是是直接用 HTML 做的点击此处查看 ,几个星期前,我用 typescript 和 canvas 重构了一下, snow效果是一个很简单的效果,但是用来练手还是 ...

  6. 台哥原创:java 连连看源码

    2010年,迷上了玩连连看 随手就做了这个,正好手头有这些图片素材 ​ 游戏启动时,界面先铺上了一层透明幕布,然后这些兵器图片交替从上到下,从左到右出现.. ​ 鼠标停在兵器格子上时,所在格子会有红色 ...

  7. 洛谷2019 3月月赛 T1

    题干 2019第一次月赛 我只有255pts T1还是比较水的... 海星 T1一道简单的模拟(就是有坑..导致很多人不能一次性AC 比如说我) _3个坑点 1.位数问题 2.-0 3.0... #i ...

  8. 洛谷 P1064 金明的预算方案(有依赖的背包问题)

    题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间金明自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:“你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过N元钱就行”.今 ...

  9. 字符类型C++(ascll码表)

    ascll码: 序号 字符 序号 字符 序号 字符 序号 字符 序号 字符 序号 字符 32 空格 48 0 64 @ 80 P 96 ` 112 p 33 ! 49 1 65 A 81 Q 97 a ...

  10. centos 7 配置php

    对于我们的目的而言,安装 Apache 只需要在 CentOS 命令终端敲入这条命令就行了: $ sudo yum install httpd $ sudo systemctl enable http ...