poj3280 Cheapest Palindrome

思路:

区间dp。添加和删除本质相同。

实现:

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 int n,m;
 5 int cost[30];
 6 int dp[2005][2005];
 7 int main()
 8 {
 9     string s;
10     int x,y;
11     char z;
12     cin >> n >> m >> s;
13     for(int i = 0; i < n; i++)
14     {
15         cin >> z >> x >> y;
16         cost[z - ‘a‘] = min(x, y);
17     }
18     for(int i = m-1; i >= 0; i--)
19     {
20         for(int j = i+1; j < m; j++)
21         {
22             if(s[i] == s[j])
23             {
24                 dp[i][j] = dp[i+1][j-1];
25             }
26             else
27             {
28                 dp[i][j] = min(dp[i+1][j] + cost[s[i] - ‘a‘],
29                                dp[i][j-1] + cost[s[j] - ‘a‘]);
30             }
31         }
32     }
33     cout << dp[0][m-1] << endl;
34     return 0;
35 } 
时间: 2024-10-24 20:05:03

poj3280 Cheapest Palindrome的相关文章

POJ3280 Cheapest Palindrome 【DP】

Cheapest Palindrome Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6013   Accepted: 2933 Description Keeping track of all the cows can be a tricky task so Farmer John has installed a system to automate it. He has installed on each cow a

POJ 3280 Cheapest Palindrome(区间DP)

 Cheapest Palindrome Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6894   Accepted: 3344 Description Keeping track of all the cows can be a tricky task so Farmer John has installed a system to automate it. He has installed on each co

poj 3280 Cheapest Palindrome(区间DP)

Cheapest Palindrome Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5995   Accepted: 2922 Description Keeping track of all the cows can be a tricky task so Farmer John has installed a system to automate it. He has installed on each cow a

POJ 3280 Cheapest Palindrome DP题解

看到Palindrome的题目,首先想到的应该是中心问题,然后从中心出发,思考如何解决. DP问题一般是从更加小的问题转化到更加大的问题,然后是从地往上 bottom up地计算答案的. 能得出状态转移方程就好办了,本题的状态转移方程是: if (cowID[i] == cow{j]) tbl[id][i] = tbl[id][i+1];//相等的时候无需改动 else tbl[id][i] = min(tbl[!id][i+1] + cost[cowID[i]-'a'], tbl[!id][i

POJ 3280:Cheapest Palindrome 区间DP好题

Cheapest Palindrome 题目链接: http://poj.org/problem?id=3280 题意: 给出一个只由小写字母组成的串,可以添加或删除一些字母(添加和删除都需要花费且花费不同),求将这个串改变成一个回文串的最小花费. 题解: 设dp[i][j]是将区间[i,j]改变成回文串的最小花费,则两种情况 ①显而易见,当s[i]==s[j]时,dp[i][j]=dp[i+1][j-1] ②当s[i]!=s[j]时,dp[i][j]有四种可能的取值,区间 [i,j-1] 删除

POJ 3280 Cheapest Palindrome(DP)

题目链接 题意 :给你一个字符串,让你删除或添加某些字母让这个字符串变成回文串,删除或添加某个字母要付出相应的代价,问你变成回文所需要的最小的代价是多少. 思路 :DP[i][j]代表的是 i 到 j 这一段位置变成回文所需的最小的代价. 1 //3280 2 #include <stdio.h> 3 #include <string.h> 4 #include <iostream> 5 6 using namespace std ; 7 8 char sh[2100]

【POJ】3280 Cheapest Palindrome(区间dp)

Cheapest Palindrome Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10943   Accepted: 5232 Description Keeping track of all the cows can be a tricky task so Farmer John has installed a system to automate it. He has installed on each cow

[poj]3280 Cheapest Palindrome 题解

[poj]3280 Cheapest Palindrome 区间dp 题意: 给你长度为m的字符串,其中有n种字符,每种字符都有两个值,分别是插入这个字符的代价,删除这个字符的代价,让你求将原先给出的那串字符变成一个回文串的最小代价. M<=2000 设 dp[i][j] 为区间 i~j 的回文串的最小代价 现在考虑怎样从别的状态转移到 区间i~j 三种情况 首先 str[i]==str[j] 那么 dp[i][j] = dp[i+1][j-1] 其次 (i+1)~j 是一个回文串 dp[i][

Cheapest Palindrome.(POJ-3280)

经典DP.影响决策的是字符串的内容.而添加和删除字符本质上是一样的,我们不管选择哪一种都可以,所以只需要取两者中费用最小的. 状态转移方程就是: if(s[i]==s[j]) dp[i][j] = dp[i+1][j-1]; else dp[i][j] = min(dp[i+1][j]+w[s[i]-'a'],dp[i][j-1]+w[s[j]-'a']); 其中dpd[i][j]表示从字符串的i-j段为回文串时的最小值. 值得注意的是i需要反向枚举,j需要正向枚举,这是由状态转移的顺序决定的.