[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][j] = dp[i+1][j] + (add[str[i]] / del[str[i]])

最后 i~(j-1) 是一个回文串 dp[i][j] = dp[i][j-1] + (add[str[j]] / del[str[j]])

代码:

 1 #include<cmath>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<iostream>
 6 #include<algorithm>
 7 #define APART puts("----------------------")
 8 #define debug 1
 9 #define FILETEST 1
10 #define inf 2010
11 #define ll long long
12 #define ha 998244353
13 #define INF 0x7fffffff
14 #define INF_T 9223372036854775807
15 #define DEBUG printf("%s %d\n",__FUNCTION__,__LINE__)
16
17 namespace chino{
18
19 inline void setting(){
20 #if FILETEST
21     freopen("_test.in", "r", stdin);
22     freopen("_test.me.out", "w", stdout);
23 #endif
24     return;
25 }
26
27 inline int read(){
28     char c = getchar(), up = c; int num = 0;
29     for(; c < ‘0‘ || c > ‘9‘; up = c, c = getchar());
30     for(; c >= ‘0‘ && c <= ‘9‘; num = (num << 3) + (num << 1) + (c ^ ‘0‘), c = getchar());
31     return  up == ‘-‘ ? -num : num;
32 }
33
34 int n, m;
35 char s[inf];
36 int add[inf], del[inf];
37 int dp[inf][inf];
38
39 inline int main(){
40     n = read(), m = read();
41     scanf("%s", s + 1);
42     for(int i = 1; i <= n; i++){
43         char c = 0;
44         std::cin >> c;
45         add[c * 1] = read();
46         del[c * 1] = read();
47     }
48     int len = strlen(s + 1);
49     for(int i = 2; i <= len; i++){
50         dp[i][i] = 0;
51         for(int j = i - 1; j; j--){
52             dp[j][i] = INF;
53             if(s[i] == s[j])
54                 dp[j][i] = dp[j + 1][i - 1];
55             dp[j][i] = std::min (dp[j][i] , dp[j + 1][i] + std::min (add[s[j] * 1], del[s[j] * 1]));
56             dp[j][i] = std::min (dp[j][i] , dp[j][i - 1] + std::min (add[s[i] * 1], del[s[i] * 1]));
57         }
58     }
59     printf("%d\n", dp[1][len]);
60     return 0;
61 }
62
63 }//namespace chino
64
65 int main(){return chino::main();}

原文地址:https://www.cnblogs.com/chiarochinoful/p/problem-poj-3280.html

时间: 2024-10-08 10:13:45

[poj]3280 Cheapest Palindrome 题解的相关文章

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)

题目链接 题意 :给你一个字符串,让你删除或添加某些字母让这个字符串变成回文串,删除或添加某个字母要付出相应的代价,问你变成回文所需要的最小的代价是多少. 思路 :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: 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 动态规划法题解

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 an electronic ID tag that the system will read as the cows pass by a scanner. Each ID tag's contents are

POJ 3280 —— Cheapest Palindrome

题目:http://poj.org/problem?id=3280 经典的区间DP #include <iostream> #include <cstdio> #include <cstring> #include <string> using namespace std; char s[2005], ch[2]; int cost[26]; int dp[2005][2005]; int main () { int n, m, c1, c2; scanf(

POJ 3280 Cheapest Palindrome(区间DP求改成回文串的最小花费)

题目链接:http://poj.org/problem?id=3280 题目大意:给你一个字符串,你可以删除或者增加任意字符,对应有相应的花费,让你通过这些操作使得字符串变为回文串,求最小花费.解题思路:比较简单的区间DP,令dp[i][j]表示使[i,j]回文的最小花费.则得到状态转移方程: dp[i][j]=min(dp[i][j],min(add[str[i]-'a'],del[str[i]-'a'])+dp[i+1][j]); dp[i][j]=min(dp[i][j],min(add[

(中等) POJ 3280 Cheapest Palindrome,DP。

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 an electronic ID tag that the system will read as the cows pass by a scanner. Each ID tag's contents are

POJ 3280 Cheapest Palindrome

区间DP. dp[i][j]表示把区间[i,j]变成回文的最小花费. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; long long INF=999999999999; const int maxn=2000+10; int q,n; char s[maxn]; long long cost[30]; long long