洛谷P2890 [USACO07OPEN]Cheapest Palindrome G 题解

洛谷P2890 [USACO07OPEN]Cheapest Palindrome G 题解

题目链接:P2890 [USACO07OPEN]Cheapest Palindrome G

题意

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.

字串S长M,由N个小写字母构成。欲通过增删字母将其变为回文串,增删特定字母花费不同,求最小花费。

显然,一个串的最小花费可以看作:从串内某个字符开始,一边修改,一边在左右添加原串中相邻的字符,最后总的花费

如果用线性dp,会丢失串的左右端点信息,而且无法从子串转移

因此考虑使用区间dp(其实上面都是废话,题写多了这种就是一眼的事情 qwq)

d p [ i ] [ j ] dp[i][j] dp[i][j] 表示区间 [ i , j ] [i,j] [i,j] 被修改好的最小花费

不难发现, i = j i=j i=j 时, d p [ i ] [ j ] dp[i][j] dp[i][j]

i ≠ j i \ne j i=j 时,

  • s [ i ] = s [ j ] s[i]=s[j] s[i]=s[j] ,显然有
    d p [ i ] [ j ] = min ⁡ ( d p [ i ] [ j ] , d p [ i + 1 ] [ j − 1 ] ) dp[i][j]=\min(dp[i][j],dp[i+1][j-1]) dp[i][j]=min(dp[i][j],dp[i+1][j1])
    注意特判 j − i = 1 j-i=1 ji=1 的情况

  • s [ i ] ≠ s [ j ] s[i] \ne s[j] s[i]=s[j]

    考虑 [ i + 1 , j ] [i+1,j] [i+1,j] 的转移,显然我们有两种选择,即

    • 删除左侧新加上的 s [ i ] s[i] s[i]
    • 右侧再添加一个 s [ i ] s[i] s[i]

    而这个选择并不会影响后续的转移,所以我们只要选个 min ⁡ \min min 就好了

    [ i , j − 1 ] [i,j-1] [i,j1] 的转移同理

    故有转移方程
    d p [ i ] [ j ] = min ⁡ ( d p [ i + 1 ] [ j ] + val [ s [ i ] ] , d p [ i ] [ j − 1 ] + val [ s [ j ] ] ) dp[i][j]=\min(dp[i+1][j]+\text{val}[s[i]],dp[i][j-1]+\text{val}[s[j]]) dp[i][j]=min(dp[i+1][j]+val[s[i]],dp[i][j1]+val[s[j]])
    val [ s [ i ] ] \text{val}[s[i]] val[s[i]] 表示 s [ i ] s[i] s[i] 字符的最小花费,就是上面说的那个 min ⁡ \min min

然后就转移就完了

代码:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
#define N (int)(2e3+15)

int n,m;
char s[N],ch;
int v[32],dp[N][N];
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    // freopen("check.in","r",stdin);
    // freopen("check.out","w",stdout);
    cin >> m >> n >> (s+1);
    for(int i=1,a,b; i<=m; i++)
    {
        cin >> ch >> a >> b;
        v[ch-'a']=min(a,b);
    }
    memset(dp,0x3f,sizeof(dp));
    for(int i=1; i<=n; i++) dp[i][i]=0;
    for(int len=2; len<=n; len++)
        for(int i=1,j=i+len-1; j<=n; i++,j++)
        {
            dp[i][j]=min(dp[i+1][j]+v[s[i]-'a'],dp[i][j-1]+v[s[j]-'a']);
            if(s[i]==s[j])
            {
                if(len==2)dp[i][j]=0;
                else dp[i][j]=min(dp[i][j],dp[i+1][j-1]);
            }
        }
    cout << dp[1][n] << endl;
    return 0;
}

转载请说明出处

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值