CF1096:D. Easy Problem(DP)
Vasya is preparing a contest, and now he has written a statement for an easy problem. The statement is a string of length n
consisting of lowercase Latin latters. Vasya thinks that the statement can be considered hard if it contains a subsequence hard; otherwise the statement is easy. For example, hard, hzazrzd, haaaaard can be considered hard statements, while har, hart and drah are easy statements.
Vasya doesn't want the statement to be hard. He may remove some characters from the statement in order to make it easy. But, of course, some parts of the statement can be crucial to understanding. Initially the ambiguity of the statement is 0
, and removing i-th character increases the ambiguity by ai (the index of each character is considered as it was in the original statement, so, for example, if you delete character r from hard, and then character d, the index of d is still 4
even though you delete it from the string had).
Vasya wants to calculate the minimum ambiguity of the statement, if he removes some characters (possibly zero) so that the statement is easy. Help him to do it!
Recall that subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.
The first line contains one integer n
(1≤n≤105
) — the length of the statement.
The second line contains one string s
of length n
, consisting of lowercase Latin letters — the statement written by Vasya.
The third line contains n
integers a1,a2,…,an (1≤ai≤998244353
).
Print minimum possible ambiguity of the statement after Vasya deletes some (possibly zero) characters so the resulting statement is easy.
6
hhardh
3 2 9 11 7 1
5
8
hhzarwde
3 2 6 9 4 8 7 1
4
6
hhaarr
1 2 3 4 5 6
0
In the first example, first two characters are removed so the result is ardh.
In the second example, 5
-th character is removed so the result is hhzawde.
In the third example there's no need to remove anything.
题意:给定一个字符串,每个字符自带权值,让你删去一些,使得不存在子序列“hard”,问最下的权值是多少。
思路:因为有顺序问题,所以我们记录维护到当前最长的前缀的代价。1对应h,2对应ha,3对应har,4对应hard,然后就不难写出方程了。
(复杂度O(5N),比赛时写了个2进制,复杂度O(16N);傻了
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
#define ll long long
const int maxn=;
int a[maxn];ll dp[maxn][],ans; char c[maxn]; int Laxt[maxn];
int id(char s){
if(s=='h') return ;
if(s=='a') return ;
if(s=='r') return ;
if(s=='d') return ;
return -;
}
void ADD(ll &x,ll y){
if(y==-) return ;
if(x==-) x=y;
else x=min(x,y);
}
int main()
{
int N;
scanf("%d%s",&N,c+); memset(dp,-,sizeof(dp));
rep(i,,N) scanf("%d",&a[i]);
dp[][]=;
rep(i,,N){
int p=id(c[i]);
if(p==-) {
rep(j,,) dp[i][j]=dp[i-][j];
continue;
}
if(dp[i-1][p-1]!=-1) ADD(dp[i][p-],dp[i-][p-]+a[i]);
ADD(dp[i][p],dp[i-][p-]);
rep(j,,){
if(j==p-) continue;
ADD(dp[i][j],dp[i-][j]);
}
}
ans=1LL<<; rep(i,,) if(dp[N][i]!=-) ans=min(ans,dp[N][i]);
printf("%lld\n",ans);
return ;
}
CF1096:D. Easy Problem(DP)的更多相关文章
- D. Easy Problem dp(有衔接关系的dp(类似于分类讨论) )
D. Easy Problem dp(有衔接关系的dp(类似于分类讨论) ) 题意 给出一个串 给出删除每一个字符的代价问使得串里面没有hard的子序列需要付出的最小代价(子序列不连续也行) 思路 要 ...
- Codeforces 1096D - Easy Problem - [DP]
题目链接:http://codeforces.com/problemset/problem/1096/D 题意: 给出一个小写字母组成的字符串,如果该字符串的某个子序列为 $hard$,就代表这个字符 ...
- CF1096D Easy Problem(DP)
题意:给出一个字符串,去掉第i位的花费为a[i],求使字符串中子串不含hard的最小代价. 题解:这题的思路还是比较套路的, dp[i][kd]两维,kd=0表示不含d的最小花费,1表示不含rd ...
- CF1096D Easy Problem
题目地址:CF1096D Easy Problem 比赛时高二dalaoLRZ提醒我是状压,然而,我还是没AC (汗 其实是一道很基础的线性dp \(f_{i,j}\) 表示序列第 \(i\) 个字符 ...
- HDU 4359——Easy Tree DP?——————【dp+组合计数】
Easy Tree DP? Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- 线段树:CDOJ1591-An easy problem A (RMQ算法和最简单的线段树模板)
An easy problem A Time Limit: 1000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Pr ...
- HDU 4359 Easy Tree DP?
Easy Tree DP? Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- UVA-11991 Easy Problem from Rujia Liu?
Problem E Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for ...
- An easy problem
An easy problem Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Sub ...
随机推荐
- SetCommMask
SetCommMask 用途:设置串口通信事件 原型:BOOL SetCommMask(HANDLE hFile, //标识通信端口的句柄 DWORD dwEvtMask ...
- html & js 单双引号
1.html使用双引号,嵌套亦如此,表示dom元素的属性 <input value="Test" type="button" onclick=" ...
- Oracle学习笔记(一)——并发与锁
1 并发 多用户数据库管理系统的一个主要任务是对 并发(concurrency)进行控制,即对多个用户同时访问同一数据进行控制.当缺乏有效的并发控制时,修改数据的操作就不能保证正常,从而危害数据完整性 ...
- WPF如何给窗口Window增加阴影效果
https://blog.csdn.net/w_sx12553/article/details/45072861
- 《图解Http》 10,11章:构建Web的技术, Web的攻击技术
10.2动态HTML 通过调用客户端脚本语言js,实现对web页面的动态改造.利用DOM文档对象模型,指定想发生变化的元素. 10.22 更容易控制的DOM 使用DOM可以将HTML内的元素当作对象操 ...
- 快速读入fread
struct FastIO { static const int S = 1e7; int wpos; char wbuf[S]; FastIO() : wpos(0) {} inline int x ...
- ubuntu谷歌浏览器安装
sudo apt-get install chromium-browser-dbg 然后按照规定就好了.
- UVA-1220 Party at Hali-Bula (树的最大独立集)
题目大意:数的最大独立集问题.特殊在要求回答答案是否唯一. 题目分析:定义状态dp(i,1),dp(i,0)分别表示以i为根节点的子树选不选i最多可选的人数,f(i,1),f(i,0)分别表示以i为根 ...
- Android studio的 repositories配置多个url
buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2. ...
- SSM框架整合(实现从数据库到页面展示)
SSM框架整合(实现从数据库到页面展示) 首先创建一个spring-web项目,然后需要配置环境dtd文件的引入,环境配置,jar包引入. 首先让我来看一下ssm的基本项目配件.(代码实现) 1.首先 ...