标签:color type long span 实现 end ++ while 表示
思路:
dp[i][j]表示到第i个位置为止不能进入状态j所需要的最小代价。
实现:
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 typedef long long ll; 5 const ll INF = 0x3f3f3f3f3f3f3f3f; 6 7 ll a[100005], dp[100005][5]; 8 9 int main() 10 { 11 int n; string s; 12 map<char, int> mp; 13 mp[‘h‘] = 1; mp[‘a‘] = 2; mp[‘r‘] = 3; mp[‘d‘] = 4; 14 while (cin >> n >> s) 15 { 16 for (int i = 0; i < n; i++) cin >> a[i]; 17 dp[0][0] = INF; 18 for (int i = 1; i <= 4; i++) dp[0][i] = 0; 19 for (int i = 1; i <= n; i++) 20 { 21 for (int j = 0; j <= 4; j++) dp[i][j] = dp[i - 1][j]; 22 if (mp.count(s[i - 1])) 23 { 24 int tmp = mp[s[i - 1]]; 25 dp[i][tmp] = min(dp[i - 1][tmp - 1], dp[i - 1][tmp] + a[i - 1]); 26 } 27 } 28 ll ans = INF; 29 for (int i = 0; i <= 4; i++) ans = min(ans, dp[n][i]); 30 cout << ans << endl; 31 } 32 return 0; 33 }
标签:color type long span 实现 end ++ while 表示
原文地址:https://www.cnblogs.com/wangyiming/p/10200171.html