标签:des style blog http color os io for
Cheapest Palindrome
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 5627 Accepted: 2734
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 currently a single string with length M (1 ≤ M ≤ 2,000) characters drawn from an alphabet of N (1 ≤ N ≤ 26) different symbols (namely, the lower-case roman alphabet).
Cows, being the mischievous creatures they are, sometimes try to spoof the system by walking backwards. While a cow whose ID is "abcba" would read the same no matter which direction the she walks, a cow with the ID "abcb" can potentially register as two different IDs ("abcb" and "bcba").
FJ would like to change the cows‘s ID tags so they read the same no matter which direction the cow walks by. For example, "abcb" can be changed by adding "a" at the end to form "abcba" so that the ID is palindromic (reads the same forwards and backwards). Some other ways to change the ID to be palindromic are include adding the three letters "bcb" to the begining to yield the ID "bcbabcb" or removing the letter "a" to yield the ID "bcb". One can add or remove characters at any location in the string yielding a string longer or shorter than the original string.
Unfortunately as the ID tags are electronic, each character insertion or deletion has a cost (0 ≤ cost ≤ 10,000) which varies depending on exactly which character value to be added or deleted. Given the content of a cow‘s ID tag and the cost of inserting or deleting each of the alphabet‘s characters, find the minimum cost to change the ID tag so it satisfies FJ‘s requirements. An empty ID tag is considered to satisfy the requirements of reading the same forward and backward. Only letters with associated costs can be added to a string.
Input
Line 1: Two space-separated integers: N and M
Line 2: This line contains exactly M characters which constitute the initial ID string
Lines 3..N+2: Each line contains three space-separated entities: a character of the input alphabet and two integers which are respectively the cost of adding and deleting that character.
Output
Line 1: A single line with a single integer that is the minimum cost to change the given name tag.
Sample Input
3 4
abcb
a 1000 1100
b 350 700
c 200 800
Sample Output
900
Hint
If we insert an "a" on the end to get "abcba", the cost would be 1000. If we delete the "a" on the beginning to get "bcb", the cost would be 1100. If we insert "bcb" at the begining of the string, the cost would be 350 + 200 + 350 = 900, which is the minimum.
Source
USACO 2007 Open Gold
题目链接:http://poj.org/problem?id=3280
继续动态规划
题目解析:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <algorithm> 4 #include <iostream> 5 using namespace std; 6 class DP { 7 public: 8 void init(); 9 void cal(); 10 private: 11 enum { 12 MAXN = 26, 13 MAXM = 2000}; 14 /*static const int MAXN = 26; 15 static const int MAXM = 2000;*/ 16 static int dp_[MAXM + 2][MAXM + 2]; 17 //动态规划的数组,横下标表示从头到此的字符串, 18 //纵下标表示从此到尾部的字符串。 19 //对于一个不对称的字符串,分别处理左右两边的子串使之平衡 20 int n_, m_, max_m_; 21 int cost_[MAXN]; 22 char str_[MAXM + 2]; 23 int minco_[MAXM + 2]; 24 }; 25 int DP::dp_[MAXM + 2][MAXM + 2] = {0}; 26 void DP::init() { 27 scanf("%d %d %s", &n_, &m_, str_ + 1); 28 char c[2]; 29 max_m_ = m_ + 1; 30 for(int i = 1; i < max_m_; i++) 31 str_[i] -= ‘a‘; 32 //对于字符串中每一个字符,抽象解释为0-25 33 int tmp1, tmp2; 34 for(int i = 0; i < n_; i++) { 35 scanf("%s %d %d", c, &tmp1, &tmp2); 36 cost_[c[0] - ‘a‘] = min(tmp1, tmp2); 37 //对于单个对称字符的处理,加一个或减一个 38 //该字符构成对称,结果一样,取消耗少的处理办法 39 } 40 } 41 void DP::cal() { 42 minco_[0] = 0; 43 for(int i = 1; i < max_m_; i++) { 44 minco_[i] = minco_[i - 1] + 45 cost_[str_[i]]; 46 }//对于一段连续的字符子串,将它们逆向对称处理的最小消耗 47 for(int i = 0; i < max_m_; i++) { 48 dp_[0][i + 1] = minco_[m_] - minco_[i]; 49 //对于左边的字符子串为0时,单独对称处理右子串的最小消耗 50 dp_[i][max_m_] = minco_[i]; 51 //对于右边的字符子串为0时,道理一样 52 } 53 //已知边界条件 54 for(int i = 1; i < max_m_; i++) { 55 for(int j = m_; j > i; j--) { 56 int opi = dp_[i - 1][j] + cost_[str_[i]]; 57 int opj = dp_[i][j + 1] + cost_[str_[j]]; 58 int op = min(opi, opj); 59 if(str_[i] == str_[j]) 60 op = min(op, dp_[i - 1][j + 1]); 61 dp_[i][j] = op; 62 } 63 } 64 //每加入一个字符进行处理,可以处理左子串,右子串, 65 //若左右两端新加字符相同,则可以直接不用处理, 66 //三种状态取小者 67 int res = 0x7fffffff; 68 for(int i = 0; i < m_; i++) { 69 res = min(res, dp_[i][i + 1]); 70 res = min(res, dp_[i][i + 2]); 71 } 72 res = min(res, dp_[m_][max_m_]); 73 printf("%d\n", res); 74 //对于最终结果,必然要覆盖原来整个字符串,或者中间隔一个中心字符, 75 //对于右子串为空的情况,没有中心字符的情况,单独处理 76 } 77 int main(void) 78 { 79 DP dp; 80 dp.init(); 81 dp.cal(); 82 return 0; 83 }
该题必须注意到从非回文子串到回文子串的处理,对于同一个字母的对称处理,加减操作具有相同效果。
之后相对于已知的边界条件,推出动态规划的计算表达式就比较方便了
标签:des style blog http color os io for
原文地址:http://www.cnblogs.com/ckLXHL/p/3873990.html