Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
[AC]感觉有点像最回文子串和编辑距离的结合,二重循环,dp解法#include <iostream> #include <string> #include <vector> using namespace std; int dp[2008][2008]; int main() { int n, m; cin >> n >> m; vector<int> cost(26); string s; cin >> s; char c; int a, b; for (int i = 0; i < n; ++i) { cin >> c >> a >> b; cost[(c - 'a')] = min(a, b); } int j; for (int k = 1; k < m; ++k) { for (int i = 0; i < m - 1; ++i) { j = i + k; if (j < m) { dp[i][j] = min(dp[i + 1][j] + cost[s[i] - 'a'], dp[i][j - 1] + cost[s[j] - 'a']); if (s[i] == s[j]) dp[i][j] = min(dp[i][j], dp[i + 1][j - 1]); } } } cout << dp[0][m - 1] << endl; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator