zoukankan      html  css  js  c++  java
  • CF 1096D Easy Problem [动态规划]

    题目链接:http://codeforces.com/problemset/problem/1096/D

    题意:给你一个长为 nn 的字符串 ss 以及 a_{1..n}a 1..n ​ ,删去第 ii 个字符的代价为 a_ia i ​ ,你需要删去一些字符(如果一开始就符合条件当然可以不删)使得剩下的串中不含子序列 "hard",求最小代价。 子序列不需要连续。给你一个长为 nn 的字符串 ss 以及 a_{1..n}a 1..n ​ ,删去第 ii 个字符的代价为 a_ia i ​ ,你需要删去一些字符(如果一开始就符合条件当然可以不删)使得剩下的串中不含子序列 "hard",求最小代价。 子序列不需要连续。

    有一长度为n的字符串,每一字符都有一个权值,要求现在从中取出若干个字符,使得字符串中没有子串hard,(这里的子串在原串中不需要连续)。求取出字符的最小权值和。

    Vasya is preparing a contest, and now he has written a statement for an easy problem. The statement is a string of length nn 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 00, and removing ii-th character increases the ambiguity by aiai (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 44 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.

    Input

    The first line contains one integer nn (1≤n≤1051≤n≤105) — the length of the statement.

    The second line contains one string ss of length nn, consisting of lowercase Latin letters — the statement written by Vasya.

    The third line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤9982443531≤ai≤998244353).

    Output

    Print minimum possible ambiguity of the statement after Vasya deletes some (possibly zero) characters so the resulting statement is easy.

    思路:动态规划。

    只要将能构成hard字符串(子串)的字符串(主串的某个子串,这个子串包含前面的那个子串)中的最后一个d之前删除同种类的字符(h,a,r,d中的某一个)使其不能构成hard。

    • h的策略只能是消灭前面的h
    • a的策略是将前面的h消灭,或者是以最优策略消灭前面顺序符合的 a或者ha
    • 同理,r的策略是使用a的策略,或者是以最优策略消灭har或者r
    • d同理。

    代码:

    ​
    #include<stdio.h>
    #include<iostream>
    using namespace std;
    char s[100005];
    long long h,a,r,d;//(权值比较大,可能会爆范围)
    long long n,x;
    int main()
    {
        scanf("%lld",&n);
        getchar();
        scanf("%s",&s);
        //四个策略
        for(int i=0; i<n; i++)
        {
            scanf("%lld",&x);
            if(s[i] == 'h')
                h+=x;
            else if(s[i]=='a')
                a = min(h,a+x);
            else if(s[i]=='r')
                r=min(a,r+x);
            else if(s[i]=='d')
                d=min(r,d+x);
        }
        printf("%lld
    ",d);
        return 0;
    }
    
    ​
  • 相关阅读:
    如何在帮助页面添加测试工具
    如何给你的ASP.NET页面添加HelpPage
    各种序列化库的性能数据
    Quartz.NET配置
    T-SQL中只截取日期的日期部分和日期的时间部分
    sql 根据指定条件获取一个字段批量获取数据插入另外一张表字段中+MD5加密
    读取图片数据流转换成图片
    T-SQL Transact-SQL 编程
    Python 链接Mysql数据库
    c 生成随机不重复的整数序列
  • 原文地址:https://www.cnblogs.com/yyaoling/p/12260444.html
Copyright © 2011-2022 走看看