最便宜的回文----POJ3280Cheapest Palindrome(含翻译)

Cheapest Palindrome

Time Limit: 2000MSMemory Limit: 65536K
Total Submissions: 20324Accepted: 9382

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

最便宜的回文

时限:2000MS内存限制:65536K
提交总数:20324接受: 9382

描述

跟踪所有奶牛可能是一项棘手的任务,因此农场主约翰安装了一个系统来自动化它。他在每头奶牛上安装了一个电子ID标签,当奶牛通过扫描仪时,系统会读取该标签。每个ID标签的内容目前都是一个长度为M(1≤M≤2,000)字符的单个字符串,这些字符来自N(1≤N ≤ 26)不同符号(即小写罗马字母)的字母表。

牛,作为淘气的生物,有时会试图通过倒退来欺骗系统。虽然ID为“abcba”的奶牛无论走哪个方向都会读相同,但ID为“abcb”的奶牛可能会注册为两个不同的ID(“abcb”和“bcba”)。

FJ想改变奶牛的ID标签,这样无论奶牛走过哪个方向,它们都能读到相同的内容。例如,可以通过在末尾添加“a”来更改“abcb”以形成“abcba”,以便ID是回文的(向前和向后读取相同)。将 ID 更改为回文的其他一些方法包括在开头添加三个字母“bcb”以产生 ID “bcbabcb”或删除字母“a”以产生 ID “bcb”。可以在字符串中的任何位置添加或删除字符,从而生成比原始字符串长或短的字符串。

遗憾的是,由于 ID 标签是电子的,因此每个字符插入或删除都有成本(0 ≤成本≤ 10,000),具体取决于要添加或删除的字符值。给定奶牛ID标签的内容以及插入或删除每个字母表字符的成本,找到更改ID标签的最低成本,使其满足FJ的要求。空 ID 标签被认为满足向前和向后读取相同的要求。只能将具有相关成本的字母添加到字符串中。

输入

第 1 行:两个空格分隔的整数:N 和 M 第 2 行:此行正好包含构成初始 ID 字符串
的 M
个字符 第 3 行。N+2:每行包含三个空格分隔的实体:输入字母表的一个字符和两个整数,分别是添加和删除该字符的成本。

输出

第 1 行:带有单个整数的单行,这是更改给定名称标签的最低成本。

示例输入

3 4
abcb
a 1000 1100
b 350 700
c 200 800

示例输出

900

提示

如果我们在末尾插入一个“a”以获得“abcba”,则成本将为1000。如果我们删除开头的“a”以获得“bcb”,则成本将为 1100。如果我们在字符串的开头插入“bcb”,则成本将为 350 + 200 + 350 = 900,这是最小值。

 

题解:

题目要求将初始ID字符串转化为回文字符串,每个字符有插入和删除两种操作,并给出了每个字符的插入和删除成本。

我们可以使用动态规划来解决这个问题。定义一个二维数组dp,其中dp[i][j]表示将ID的第i个字符到第j个字符之间的子串变为回文字符串的最小成本。

动态规划的状态转移方程如下:

如果s[i] == s[j],即第i个字符和第j个字符相同,那么不需要进行任何操作,dp[i][j] = dp[i+1][j-1]。

如果s[i] != s[j],那么有两个选择:

  1. 在第i个位置插入s[j],也就是将s[j]插入到子串的开头,此时dp[i][j] = dp[i][j-1] + cost_insert[s[j]-'a'],其中cost_insert[s[j]-'a']表示插入s[j]的成本。
  2. 在第j个位置删除s[i],也就是将s[i]删除,此时dp[i][j] = dp[i+1][j] + cost_delete[s[i]-'a'],其中cost_delete[s[i]-'a']表示删除s[i]的成本。

最终的答案为dp[0][m-1],也就是将整个ID字符串变为回文字符串的最小成本。

  1. 首先,读取输入的N和M,表示字符集大小和初始ID字符串的长度。

  2. 然后,读取初始ID字符串。

  3. 接着,使用循环读取每个字符及其插入和删除的成本,并将它们保存在数组w中,其中w[i]表示字符'i'的插入和删除成本之间的较小值。

  4. 初始化二维数组dp为0。

  5. 通过两层循环,我们依次计算子问题,len代表当前子问题的长度(从2开始到M)。

  6. 在每个子问题中,使用两个指针i和j来表示子问题的范围。如果s[i]=s[j],则不需要进行任何操作,dp[i][j]=dp[i+1][j-1]。

  7. 如果s[i]!=s[j],我们有两个选择:

    • 在i位置插入字符s[j],将dp[i][j]更新为dp[i][j-1]+w[s[j]-'a']。
    • 在j位置删除字符s[i],将dp[i][j]更新为dp[i+1][j]+w[s[i]-'a']。 我们选取上述两个选择中的较小值作为dp[i][j]。
  8. 最后,输出dp[0][m-1],即将整个ID字符串变为回文字符串的最小成本。

 代码:

#include<iostream>
#include<cstring>
using namespace std;

const int maxn = 2000 + 10;
int n, m, w[26], dp[maxn][maxn];
string s;

int main()
{
    // 读取N和M
    scanf("%d%d",&n,&m);
    // 读取初始ID字符串
    cin >> s;
    // 读取每个字符的插入和删除成本
    for(int i = 1; i <= n; i++){
        char c;
        cin >> c;
        int k1, k2;
        scanf("%d%d",&k1,&k2);
        w[c-'a'] = min(k1, k2);
    }
    // 初始化dp数组为0
    memset(dp, 0, sizeof(dp));
    // 动态规划
    for(int len = 2; len <= m; len++){
        for(int i = 0; i <= m - len; i++){
            int j = i + len - 1;
            if(s[i] == s[j])
                dp[i][j] = dp[i+1][j-1];
            else
                dp[i][j] = min(dp[i+1][j] + w[s[i]-'a'], dp[i][j-1] + w[s[j]-'a']);
        }
    }
    // 输出最小成本
    printf("%d", dp[0][m-1]);
    return 0;
}

 

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

善程序员文

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值