[poj]3280 Cheapest Palindrome 题解

[poj]3280 Cheapest Palindrome

Interval dp

Meaning of the questions:

You string of length m, wherein there are n characters, each character has two values, namely, the cost of this character is inserted, the cost of deleting the character string of the character so that you will find the original analysis becomes into a minimum cost string palindrome.

M<=2000

Provided dp [i] [j] is a minimum cost palindromic sequence of interval i ~ j

Now consider how transfer from other states to the interval i ~ j

three situations

First str [i] == str [j] then dp [i] [j] = dp [i + 1] [j-1]

Then (i + 1) ~ j is a palindromic sequence dp [i] [j] = dp [i + 1] [j] + (add [str [i]] / del [str [i]])

Finally i ~ (j-1) is a palindromic sequence dp [i] [j] = dp [i] [j-1] + (add [str [j]] / del [str [j]])

Code:

 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();}

 

Guess you like

Origin www.cnblogs.com/chiarochinoful/p/problem-poj-3280.html