标签:
题目大意:一个字符串,用已知的字符把它变为回文串时,代价最小是多少?其中添加一个字符或删除一个字符都有相应代价。
Input
Output
Sample Input
3 4 abcb a 1000 1100 b 350 700 c 200 800
Sample Output
900
Hint
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cstring> 4 #include <iostream> 5 using namespace std; 6 7 char str[2002]; 8 int dp[2002][2002]; 9 int cost[28]; 10 11 int main() { 12 int m, n; 13 //freopen("input.txt","r",stdin); 14 while(scanf("%d %d",&m,&n) != EOF) { 15 scanf("%s",str); 16 memset(cost, 0, sizeof(cost)); 17 for(int i = 0; i < m; i++) { 18 int a, b; 19 char c; 20 cin >> c >> a >> b; 21 cost[c-‘a‘] = min(a,b); 22 } 23 memset(dp, 0, sizeof(dp)); 24 for(int len = 1; len < n; len++) { 25 for(int i = 0, j = len; i < n && j < n; i++,j++) { 26 if(str[i] == str[j]) { 27 dp[i][j] = dp[i+1][j-1]; 28 } 29 else { 30 dp[i][j] = min(dp[i][j-1] + cost[str[j] - ‘a‘], dp[i+1][j] + cost[str[i] - ‘a‘]); 31 } 32 } 33 } 34 printf("%d\n",dp[0][n-1]); 35 36 } 37 return 0; 38 }
标签:
原文地址:http://www.cnblogs.com/jasonJie/p/5830629.html