标签:des style blog http io ar color os 使用
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 lengthM (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
Output
Sample Input
3 4 abcb a 1000 1100 b 350 700 c 200 800
Sample Output
900
Hint
Source
一看这道题总觉得是字符串处理问题,其实是需要建模动态规划法的题解。
动态规划法的建模都感觉是最难的一关了,当然最简单是参考别人的,自己建模真的很难。
本题的建模就是利用一个二维数组palin[i][j],代表j个字符,就是如果字符串的起点下标为i,那么i到i+j-1字符的最小修改值是多少。
也可以用递归的思维从这个字符串一步一步往更小的字符串递推出来。
最终优化程序,使用滚动数组变二维数组维一维。
下面程序作出详细注解:
#include <stdio.h> #include <string.h> #include <limits.h> #include <algorithm> using namespace std; const int MAX_M = 2001; const int ALP_NUM = 26; int palin[2][MAX_M];//palin[id][i],id数组代表当前d个字符段计算的最小修改值,!id数组代表d-1个字符段的计算值,那么如果id维的数组没有计算前,代表d-2个字符段计算的最小修改值 int cost[ALP_NUM];//只需要计算最小的修改值就可以了 char cow[MAX_M];//id字符串 int getMinCost(int M) { memset(palin[0], 0, sizeof(int) * (M+1)); memset(palin[1], 0, sizeof(int) * (M+1));//初始值都为零,只有一个字符的时候肯定是不需要修改的Palindrome,修改值为零 bool id = 0; for (int d = 2; d <= M; d++) { id = !id; for (int i = 0, j = d-1; j < M; i++, j++) { //只需要统一记录方法,i记录当前d个字符的修改值 //两个字符相等,那么直接等于d-2个字符的时候的修改值 if (cow[i] == cow[j]) palin[id][i] = palin[id][i+1]; else//两个字符不相等就比较处理这两个字符哪个便宜 { //palin[!id][i+1]代表i+1到i+1+d-1个字符的处理值 //故此不包含第i个字符 int c1 = palin[!id][i+1] + cost[cow[i]-'a']; //palin[!id][i]代表i到i+d-1个字符的处理值 //故此不包含第j个字符 int c2 = palin[!id][i] + cost[cow[j]-'a']; //palin[id][i]代表i到i+d个字符的处理值 palin[id][i] = min(c1, c2); } } } return palin[id][0]; } int main() { int N, M, a, d; while (scanf("%d %d", &N, &M) != EOF) { getchar(); gets(cow);//gets后面处理掉\n字符了 fill(cost, cost+ALP_NUM, 0); for (int i = 0; i < N; i++) { char ch = getchar(); scanf("%d %d", &a, &d);//插入或者删除的处理字符代价 cost[ch-'a'] = min(a, d);//只需要记录处理字符的最小代价就可以了 getchar();//处理掉后面的\n字符 } printf("%d\n", getMinCost(M)); } return 0; }
POJ 3280 Cheapest Palindrome 动态规划法题解
标签:des style blog http io ar color os 使用
原文地址:http://blog.csdn.net/kenden23/article/details/41621323