CodeForces 1096D (2020.3.15训练H题)

Problem
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.

题意:给定一个长度为n的字符串,每个位置权值为a1,a2…an(假定权值和为sum),为了不出现"hard"子序列,求最小删去字母的最小值,可以是多个

解法:可以定义一个dp[i][j]数组,表示第i个位置为第j种情况时的前i个字符最大权值(因为要求删去的最小值,那就意味着剩下的字符串权值之和最大,j=0表示未出现h,j=1表示出现h序列,j=2表示出现ha序列,j=3表示出现har序列,j=4表示出现hard序列),遍历每个字符,不同情况下更新dp[i][j]的值,最后输出sum-max(dp[n][j])就可以了(j=0.1.2.3)

ac代码:

扫描二维码关注公众号,回复: 10376587 查看本文章
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
typedef long long ll;
const ll maxn = 1e5 + 6;
char s[maxn];
ll val[maxn];
ll dp[maxn][5];
ll n;
int main()
{
	cin >> n;
	scanf("%s", s + 1);
	ll sum = 0;
	for (int i = 1; i <= n; i++)
	{
		cin >> val[i];
		sum += val[i];
	}
	for (int i = 1; i <= n; i++)
	{
		if (s[i] == 'h')
		{
			dp[i][0] = dp[i - 1][0];
			dp[i][1] = max(dp[i - 1][0], dp[i - 1][1]) + val[i];
			for (int j = 2; j <= 4; j++)
			{
				dp[i][j] = dp[i - 1][j] + val[i];
			}
		}
		else if (s[i] == 'a')
		{
			dp[i][0] = dp[i - 1][0] + val[i];
			dp[i][1] = dp[i - 1][1];
			dp[i][2] = max(dp[i - 1][1], dp[i - 1][2]) + val[i];
			for (int j = 3; j <= 4; j++)
			{
				dp[i][j] = dp[i - 1][j] + val[i];
			}
		}
		else if (s[i] == 'r')
		{
			dp[i][0] = dp[i - 1][0] + val[i];
			dp[i][1] = dp[i - 1][1] + val[i];
			dp[i][2] = dp[i - 1][2];
			dp[i][3] = max(dp[i - 1][3], dp[i - 1][2]) + val[i];
			dp[i][4] = dp[i - 1][4] + val[i];
		}
		else if (s[i] == 'd')
		{
			dp[i][0] = dp[i - 1][0] + val[i];
			dp[i][1] = dp[i - 1][1] + val[i];
			dp[i][2] = dp[i - 1][2] + val[i];
			dp[i][3] = dp[i - 1][3];
			dp[i][4] = max(dp[i - 1][4], dp[i - 1][3]) + val[i];
		}
		else
		{
			for (int j = 0; j <= 4; j++)
			{
				dp[i][j]=dp[i - 1][j] + val[i];
			}
		}
	}
	ll maxx = -999;
	for (int j = 0; j <= 3; j++)
	{
		if (dp[n][j] > maxx)
			maxx = dp[n][j];
	}
	cout << sum - maxx << endl;

}
发布了16 篇原创文章 · 获赞 21 · 访问量 1325

猜你喜欢

转载自blog.csdn.net/rainbowower/article/details/104931711