有一个长度为m的字符串,由n种小写字母组成。对应的n种字母在这个字符串加上或者减去都有相应的费用,现在要将这个字符串变成回文串,问最低消费是多少?

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.
增加或者减少最少字母使其变为回文串是一个经典的dp问题,可转换为LCS求解,显然这是一个区间dp问题;

定义dp [ i ] [ j ] 为区间 i 到 j 变成回文的最小代价。

那么对于dp[i][j]三种情况

首先:对于一个串如果s[i]==s[j],那么dp[i][j]=dp[i+1][j-1];

其次:如果dp[i+1][j]是回文串,那么dp[i][j]=dp[i+1][j]+min(add[i],del[i]);

最后,如果dp[i][j-1]是回文串,那么dp[i][j]=dp[i][j-1] + min(add[j],del[j]);

 
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
#include<iostream>
using namespace std;
const int inf =0x3f3f3f3f;
int dp[2005][2005],x[256],i,add,del,n,m;
int main()
{
char s[2005],c;
scanf("%d%d",&n,&m);
scanf("%s",s+1);
for(i=1;i<=n;i++)
{
getchar();//吸收回车
scanf("%c %d%d",&c,&add,&del);
x[c]=min(add,del);//对于拼凑回文串,加上字符和减去字符是一样的
} //所以这里选择成本小的途径
int k;
for(k=1;k<m;k++)//k次搜索,最差一个字母构成回文==每次删掉一个字母
{
for(i=1;i<=m-k;i++)//从1到m-k的长度
{
int j=i+k;
dp[i][j]=inf;//初始化
dp[i][j]=min(dp[i+1][j]+x[s[i]],dp[i][j-1]+x[s[j]]);
if(s[i]==s[j])
dp[i][j]=min(dp[i][j],dp[i+1][j-1]);
}
}
printf("%d\n",dp[1][m]);//从1到m构成回文串的最小花费
}

F - Cheapest Palindrome的更多相关文章

  1. POJ 题目3280 Cheapest Palindrome(区间DP)

    Cheapest Palindrome Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7148   Accepted: 34 ...

  2. 【POJ】3280 Cheapest Palindrome(区间dp)

    Cheapest Palindrome Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10943   Accepted: 5 ...

  3. Cheapest Palindrome(区间DP)

    个人心得:动态规划真的是够烦人的,这题好不容易写出了转移方程,结果超时,然后看题解,为什么这些题目都是这样一步一步的 递推,在我看来就是懵逼的状态,还有那个背包也是,硬是从最大的V一直到0,而这个就是 ...

  4. POJ3280 Cheapest Palindrome 【DP】

    Cheapest Palindrome Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6013   Accepted: 29 ...

  5. 【POJ - 3280】Cheapest Palindrome(区间dp)

    Cheapest Palindrome 直接翻译了 Descriptions 给定一个字符串S,字符串S的长度为M(M≤2000),字符串S所含有的字符的种类的数量为N(N≤26),然后给定这N种字符 ...

  6. [USACO07OPEN]便宜的回文Cheapest Palindrome

    字串S长M,由N个小写字母构成.欲通过增删字母将其变为回文串,增删特定字母花费不同,求最小花费.        题目描述见上            显然 这是一道区间DP 从两头DP,枚举长度啥的很套 ...

  7. Cheapest Palindrome [POJ3280] [区间DP] [经典]

    一句话题意:每个字母添加和删除都相应代价(可以任意位置 增加/删除),求把原串变成回文串的最小代价 Description 保持对所有奶牛的跟踪是一项棘手的任务,因此农场主约翰已经安装了一个系统来实现 ...

  8. [luoguP2890] [USACO07OPEN]便宜的回文Cheapest Palindrome(DP)

    传送门 f[i][j] 表示区间 i 到 j 变为回文串所需最小费用 1.s[i] == s[j] f[i][j] = f[i + 1][j - 1] 2.s[i] != s[j] f[i][j] = ...

  9. POJ 3280 Cheapest Palindrome DP题解

    看到Palindrome的题目.首先想到的应该是中心问题,然后从中心出发,思考怎样解决. DP问题通常是从更加小的问题转化到更加大的问题.然后是从地往上 bottom up地计算答案的. 能得出状态转 ...

随机推荐

  1. springboot项目打war包流程

    目前,前后端分离的架构已成主流,因此使用springboot构建应用是非常快速的,项目发布到服务器上的时候,只需要打成一个jar包,然后通过命令 : java -jar jar包名称即可启动服务了:但 ...

  2. MQ for linux安装与卸载【转】

    MQ for linux安装与卸载[转] 一.安装步骤:1. 用root帐号登录系统2. MQ安装程序需将代码安装到目录/opt/mqm下,将数据保存到目录/var/mqm下,需确保相关目录下有足够的 ...

  3. 【Sphinx】 为Python自动生成文档

    sphinx 前言 Sphinx是一个可以用于Python的自动文档生成工具,可以自动的把docstring转换为文档,并支持多种输出格式包括html,latex,pdf等 开始 建一个存放文档的do ...

  4. 使用msys2在window下构建和使用Linux的软件

    目录 前言 安装 使用 总结 前言 在window下构建Linux编译环境是很常见的,以前用过mingw弄过差不多的环境. 但是使用msys2后就根本停不下来咯,太好用咯. 安装 去官网下载吧,安装跟 ...

  5. ctfhub技能树—文件上传—双写后缀

    双写后缀绕过 用于只将文件后缀名,例如"php"字符串过滤的场合: 例如:上传时将Burpsuite截获的数据包中文件名[evil.php]改为[evil.pphphp],那么过滤 ...

  6. [Usaco2007 Dec]Building Roads 修建道路

    题目描述 Farmer John最近得到了一些新的农场,他想新修一些道路使得他的所有农场可以经过原有的或是新修的道路互达(也就是说,从任一个农场都可以经过一些首尾相连道路到达剩下的所有农场).有些农场 ...

  7. 集成 12 种协议、可于 USBC 端口的快充协议芯片IP2188

    1. 特性  支持 12 种 USB 端口快充协议  支持 USB TypeC PD2.0/PD3.0/PPS DFP 协议  支持多种充电协议(QC3.0/QC2.0,FCP,SCP, AFC,MT ...

  8. Git 创建新分支检查分支

    创建分支和切换分支,也可以称为检出分支 创建新分支 git branch branchName 切换到新分支 git checkout branchName 上面两个命令也可以合成为一个命令: git ...

  9. Py层次递进与文件修改大程序,模块,name与file

    层次的递进与返回 #输入quit的时候返回上一阶层,输入exit退出所有的循环 tag=True while tag==True: level1=input('level1:') if level1= ...

  10. 夯实基础系列一:Java 基础总结

    前言 大学期间接触 Java 的时间也不短了,不论学习还是实习,都让我发觉基础的重要性.互联网发展太快了,各种框架各种技术更新迭代的速度非常快,可能你刚好掌握了一门技术的应用,它却已经走在淘汰的边缘了 ...