Topic links: http://codeforces.com/problemset/problem/1096/D
Meaning of the questions:
Now there is a string of lower-case letters, to remove the i-th position of the string will be a [i] of the price asked to remove some characters in the string does not contain such a sequence of hard and minimum cost.
Ideas:
This title is a look at the issue of a dp, and a bit like a knapsack problem.
DP [i] First, we define the [1] to i when the representative matching prefix 'h' minimum cost does not exist
After so
Then came the problem is not deleted and the deleted. Or to see the specific bar code
1 #include <iostream> 2 #include <algorithm> 3 #include <stdlib.h> 4 #include <cstring> 5 #include <string> 6 #include <string.h> 7 #include <set> 8 #include <queue> 9 #include <stdbool.h> 10 11 #define LL long long 12 using namespace std; 13 const int maxn = 1e5 + 10; 14 LL inf = 1e15; 15 16 LL arr[maxn]; 17 char s[maxn]; 18 char ss[5] = {'p','h','a','r','d'}; 19 LL dp[maxn][5]; 20 21 int main(){ 22 int n; 23 scanf("%d",&n); 24 scanf("%s",s+1); 25 for (int i=1;i<=n;i++){ 26 scanf("%lld",&arr[i]); 27 dp[i][0] = inf; 28 } 29 if (s[1] == 'h') 30 dp[1][1] = arr[1]; 31 for (int i=2;i<=n;i++){ 32 for (int= J . 1 ; J <= . 4 ; J ++ ) { 33 is IF (S [I] == SS [J]) { // there is a, we can consider delete and does not delete the letter. If you do not delete this letter, you can not have in front of it. For example, there has been 'r', appears 'h' 'a' before we can not make it 34 is DP [I] [J] = min (DP [I- . 1 ] [J- . 1 ], DP [I- . 1 ] [J] + ARR [I]); // if delete the letter, it is directly coupled with the cost like 35 } 36 the else { 37 [ DP [I] [J] DP = [I- . 1 ] [J]; // If not, we let equal the latter appeared in front of. We later convenient output 38 } 39 } 40 } 41 printf("%lld\n",dp[n][4]); 42 return 0; 43 }