cf Educational Codeforces Round 57 D. Easy Problem

原题:
D. Easy Problem

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output

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.

Input
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).

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

Examples
input
6
hhardh
3 2 9 11 7 1
output
5
inputCopy
8
hhzarwde
3 2 6 9 4 8 7 1
output
4
input
6
hhaarr
1 2 3 4 5 6
output
0
Note

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.

中文:

给你一个字符串,长度为n,每个字符都有一个权值,现在让你去掉这个字符串中的一些字符,使得这个字符串中不存在hard这个字符串的子串,判断hard子串存在的条件是,hard字符串之间存在0到多个字符,在去除一些字符后满足不存在hard这个字符串的子串情况下,使得去掉的字符串的权值和最小。

代码:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn=1e5+5;
const ll inf = 1e18;
ll gcd(ll a,ll b)
{
    if(a%b==0)
        return b;
    return gcd(b,a%b);
}
ll dp[maxn][5],a[maxn];
int n;
string s;
string hard="hard";

int main()
{
    ios::sync_with_stdio(false);
    while(cin>>n)
    {
        cin>>s;
        for(int i=1;i<=n;i++)
            cin>>a[i];

        for(int i=0;i<=n;i++)
        {
            for(int j=0;j<5;j++)
                dp[i][j]=inf;
        }
        ll ans=inf;
        for(int i=1;i<=4;i++)
            dp[0][i]=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<5;j++)
            {
                if(s[i-1]!=hard[j-1])
                    dp[i][j]=dp[i-1][j];
                else
                    dp[i][j]=min(dp[i-1][j-1],dp[i-1][j]+a[i]);

                if(i==n)
                {
                    ans=min(ans,dp[i][j]);
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}


思路:

此题可以先优化一下,所有非hard的字母可以全去掉,出现在第一个字母h之前的字母也可以全去掉,出现在第一个h和第一个a之间的非h字母也可以全去掉,同理还可以去掉一些其他字母,最后剩下的字母可以用动态规划运算

此题关键在于相处状态保存什么

设置 d p [ i ] [ j ] dp[i][j] dp[i][j]表示前i个字母当中包含hard这个字符串前j个字符串前缀的最小权值

那么,考虑第i个字符,如果第i个字符与 h a r d [ j ] hard[j] hard[j]不同,那么有 d p [ i ] [ j ] = d p [ i − 1 ] [ j ] dp[i][j]=dp[i-1][j] dp[i][j]=dp[i1][j]表示跳过第i个字符
否则考虑第i个字符串是保留还是去掉,选择权值小的状态

如果保留第i个字符, d p [ i ] [ j ] = d p [ i − 1 ] [ j − 1 ] dp[i][j]=dp[i-1][j-1] dp[i][j]=dp[i1][j1]

如果去掉第i个字符, d p [ i ] [ j ] = d p [ i − 1 ] [ j ] + a [ i ] dp[i][j]=dp[i-1][j]+a[i] dp[i][j]=dp[i1][j]+a[i],其中 a [ i ] a[i] a[i]是第i个字符的权值

最后计算一下包含h,ha,har以及hard这四个子串的最小值,即可,因为去掉这四个子串中的任一一个都可以破坏"hard"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值