poj 3280(区间DP)

Cheapest Palindrome
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 12396   Accepted: 5873

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.

题目大意:给定一个字符串S及其长度M与S所含有的字符种数N(最多26种小写字母),然后给定这N种字母Add与Delete的代价,求将S变为回文串的最小代价和。

我在别的地方看见过这样一道题目:通过增添或者删除某些字母将一个字符串变为回文串,求最少增添或者删除次数,其实在这里增添和删除是没有区别的,我们只考虑删除就行了,对于这个模型通常有2种解法:

①将S反转得到S',然后求两者最长公共子串的长度L,再用S的长度减去L便是答案了

②用 dp[i][j] 表示 [i,j] 区间的串的最优解,

s[i]==s[j],则dp[i][j]=dp[i+1][j-1](请思考为什么)

否则 dp[i][j] = min (d[i+1][j] , dp[i][j-1]) + 1,因为s[i]!=s[j],所以必定要删去左右边界的某一个才能变成一个回文串,至于到底删去哪一个,当然是取 dp[i+1][j] 和 dp[i][j-1] 的MIN了

对于这道题目,区间dp,就同上面的第二种方法,设do[i][j]表示字符串的区间i--j之间要成为回文串所需要的最小花费。

若 s[i]==s[j]时,则dp[i][j]=dp[i+1][j-1].。例如如果我给你一个字符串abacb,当i=1,j=4时,最小的花费肯定是dp[2][3](这里的dp[2][3]是已经处理完的),因为当两边相等的时候,即首尾相等的时候,是可以去掉的,可加可不加因为要递推出来,所以要让dp[i][j]等于一个值,那就是dp[i+1][j-1]。

当首尾不相等的时候:

对于区间i-j有两种可能操作  如果i-(j-1)是回文串  则对于字符j有两种操作 删除和添加

如果(i+1)-j是回文串  则对于字符i有两种操作 删除和添加

dp[i][j]=min(dp[i][j],dp[i+1][j]+min(add[s[i]-'a'],delet[s[i]-'a']));  其中dp[i+1][j]是回文串,所以dp[i][j]的值就看这个s[i]是在i--j的区间中删去还是再 添 加,所以dp[i][j]的值就等于此次添加还是删去的最小值加上已是回文串的dp[i+1][j]的值。

dp[i][j]=min(dp[i][j],dp[i][j-1]+min(add[s[j]-'a'],delet[s[j]-'a']));意思同上

其实无论是删去还是增添,只要取最小的操作就可以了

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
#define MAXN 40005
#define mem(a,b) memset(a,b,sizeof(a));
int dp[2003][2003];
int ch[30];
int main()
{
    int n;
    int m;
    cin>>m>>n;
    string ss;
    cin>>ss;
    char c;
    for(int i=0;i<m;i++)
    {
        int ending , inserting;
        cin>>c>>inserting>>ending;
        ch[c-'a']=min(inserting,ending);
    }
    //mem(dp,0x3f3f3f3f);//此处是不能全部设为无穷大的,如果给你样例字符串是abcbb的话,dp[3][4]=350,而不是0;
    for(int i=n-1;i>=0;i--)
    {
        dp[i][i]=0;
        for(int j=i+1;j<n;j++)
        {//在这里可以   dp[i][j]=0x3f3f3f3f;(不知道为啥上面的不行),在这样写式不用,但这样:dp[i][j]=min(dp[i][j],dp[i][j-1]+ch[ss[j]-'a']);
                                                           //dp[i][j]=min(dp[i+1][j]+ch[ss[i]-'a'],dp[i][j])是必须
            dp[i][j]=min(dp[i+1][j]+ch[ss[i]-'a'],dp[i][j-1]+ch[ss[j]-'a']);
         if(ss[i]==ss[j])

                dp[i][j]=dp[i+1][j-1];
   }
    cout<<dp[0][n-1]<<endl;
    return 0;

}
或者你也可以这样写:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
#define MAXN 40005
#define mem(a,b) memset(a,b,sizeof(a));
int dp[2003][2003];
int ch[30];
int main()
{
    int n;
    int m;
    cin>>m>>n;
    string ss;
    cin>>ss;
    char c;
    for(int i=0;i<m;i++)
    {
        int ending , inserting;
        cin>>c>>inserting>>ending;
        ch[c-'a']=min(inserting,ending);
    }
    for(int i=1;i<n;i++)
    {
        for(int j=i-1;j>=0;j--)
        {
             if(ss[i]==ss[j])
                dp[i][j]=dp[i-1][j+1];
            else
             dp[i][j]=min(dp[i-1][j]+ch[ss[i]-'a'],dp[i][j+1]+ch[ss[j]-'a']);
           
        }//for(int j=i-1;j>=0;j--)//两种for循环都行,只是此时的dp[i][j]表示的是从j--i的区间元素了
        //{
        //     dp[i][j]=min(dp[i-1][j]+ch[ss[i]-'a'],dp[i][j+1]+ch[ss[j]-'a']);
        //  if(ss[i]==ss[j])
       //     dp[i][j]=dp[i-1][j+1];
       //
    }
//    for(int i=0;i<n;i++)
//    {
//        for(int j=0;j<n;j++)
//            cout<<dp[i][j]<<"\t";
//        cout<<endl;
//    }
    cout<<dp[n-1][0]<<endl;
    return 0;
}


猜你喜欢

转载自blog.csdn.net/c___c18/article/details/80887394