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

【题意】

让你将一个字符串删掉一些字符。
使得字符串中不包含子序列"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. 什么是javascript闭包?

    在我们开发中,也经常使用到闭包,但当有人问什么是闭包,就会可能说不上来.那就谈谈一些基本的: 一.理解闭包的概念, 简单说当function里嵌套function时,内部的function可以访问外部 ...

  2. PKUACM 2018 D chocolate【并查集+克鲁斯卡尔】

    传送:http://poj.openjudge.cn/practice/C18D/ 依然是课件截图 #include<iostream> #include<cstdio> #i ...

  3. noip2016 小结(ac两题+学习总结)

    NOIP2016考试小结 DAY 1 T1 题目描述 小南有一套可爱的玩具小人, 它们各有不同的职业. 有一天, 这些玩具小人把小南的眼镜藏了起来. 小南发现玩具小人们围成了一个圈,它们有的面朝圈内, ...

  4. vbnet 进程监控,监控Acad开启的数量,并且添加到开机启动

    1# 自定义函数,添加到注册表 Public Sub StartRunRegHKLM() REM HKEY_LOCAL_MACHINE \ SOFTWARE \ WOW6432Node \ Micro ...

  5. Django day 34 过滤课程,登录,redis,python操作redis

    一:过滤课程, 二:登录 三:redis, 四:python操作redis

  6. python使用mysql connection获取数据感知不到数据变化问题

    在做数据同步校验的时候,需要从mysql fetch数据和hbase的数据进行对比,发现即使mysql数据变化了,类似下面的代码返回的值还是之前的数据.抽取的代码大概如下: import MySQL ...

  7. CF861B Which floor?

    思路: 暴力枚举. 实现: #include <bits/stdc++.h> using namespace std; int n, m, x, y; bool check(int x, ...

  8. P1160 队列安排

    题目描述 一个学校里老师要将班上N个同学排成一列,同学被编号为1-N,他采取如下的方法: 1.先将1号同学安排进队列,这时队列中只有他一个人: 2.2-N号同学依次入列,编号为i的同学入列方式为:老师 ...

  9. 史上最大型广告欺诈活动Methbot:黑客是如何每天赚到500万美元的

    根据国外安全专家的最新报告,有一群黑客正在对美国的知名企业和媒体机构进行广告欺诈活动,而这群黑客每天都可以从中赚取三百万到五百万美金. 是的,你没看错,这绝对是人类历史上最牛X的恶意广告欺诈活动!不过 ...

  10. 【笔记JS/HTML/CSS】用div实现个性化button,背景半透明

    html中的button默认样式..不太能看,如果调一调背景色和字体的话也挺适合简洁的页面设计 于是决定配合JS,用html中的div完成button 最终结果图: html代码:(first_pas ...