Cheapest Palindrome(区间DP,好题)

来源:互联网 发布:凯撒博尔吉亚 知乎 编辑:程序博客网 时间:2024/06/05 04:54
Cheapest Palindrome
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 6904 Accepted: 3348

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 4abcba 1000 1100b 350 700c 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

题意:

给你一个字符串,添加或删除某字符都有相应的费用,让你添加或删除一些字符使得原字符串变为回文字符串,求最少需要的费用。


分析:

这题真想了很久,感觉无从下手。很容易知道可以利用动态规划解决,但就连添加或删除某字符这个操作都是很难的,更不要谈该如何添加或删除字符串使得成为一个回文字符串。但其实根本不需要实际操作这些。。。我们可以仔细分析一下回文字符串的特性,比如一个字符串"a"和字符串"b"的费用都是0因为它们已经回文,而字符串"ab"的费用是添加/删除'a'或'b'产生的费用。我们并不需要实际操作,甚至不需要知道是删除还是添加,因为可以发现对于一个已经是回文的字符串来说,在其前面或后面加一个字符会出现两种情况:1.还是回文字符串,此时第一个字符和最后一个字符相等。2.变为非回文字符串,这样就需要再在另一边添加同样字符或者删除这个字符以此维持回文,而到底选择哪种只取决于是添加这个字符的费用小还是删除这个字符的费用小。也就是说,我不需要实际操作,只是知道进行了一种操作,然后就能维持回文。所以我们可以由内往外推,从一个字符开始,直到推向整个字符串,推到最后也就是说所给的字符串已经变为回文字符串的最小费用求出。设dp[i][j]表示i~j区间的字符串变为回文字符串所产生的最小费用。由于是从内往外递推的,所以对于上述的情况2来说转移方程为:dp[i][j] = min(dp[i+1][j],dp[i][j-1]); 情况1的转移方程是有所不同的,因为它此时不需要进行添加/删除操作,而它的最小费用应该由区间(i-1)~(j-1)推过来(比如"aba",区间0~2是由区间1~1推来的,根本不需要添加/删除'a'的费用,假如从0~1推来,0~1已经是含添加/删除‘a'的费用了的,这样肯定最终费用不会最小),转移方程:dp[i][j] = min(dp[i][j],dp[i+1][j-1]); 也可以直接写dp[i][j] = dp[i+1][j-1]; 注意当i == j 和i+1 == j的时候i+1>j-1,所以这个要特判一下。

写完题解后感觉思路更加清楚了0-0

code:

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;typedef long long ll;const int N = 2010;int dp[N][N];int main(){    int n, m, a, b, cost[30];    char s[N], c;    scanf("%d%d", &n,&m);    scanf("%s", s);    for(int i=0; i<n; i++)    {        scanf(" %c %d %d",&c,&a,&b);        cost[c-'a'] = min(a,b);    }    for(int len=0; len<m; len++)    {        for(int i=0; i+len<m; i++)        {            int j = i+len;            dp[i][j] = min(dp[i+1][j]+cost[s[i]-'a'],dp[i][j-1]+cost[s[j]-'a']);            if(s[i] == s[j])            {                if(i == j || i+1 == j) dp[i][j] = 0;                else dp[i][j] = dp[i+1][j-1];            }        }    }    printf("%d\n", dp[0][m-1]);    return 0;}


0 0
原创粉丝点击