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.
 
题解:
  这个题目有wa了一次,才a。
  套路区间dp的解法,dp[i][j]表示把i~j改成回文的最小代价。如果s[i]==s[j]那么显然有一种转移dp[i][j]=dp[i+1][j-1],然后就是第二种可以一个一个删除或者加,dp[i][j]=dp[i+1][j]+cost[s[i]-'a'],dp[i][j]=dp[i][j-1]+cost[s[j]-'a']。
 
代码:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <iostream>
#define MAXN 2010
using namespace std;
char s[MAXN];
int dp[MAXN][MAXN];
int cost[MAXN];
int n,len; int main()
{
scanf("%d%d",&n,&len);
scanf("%s",s+);
for(int i=;i<=n;i++){
char x;scanf("%c",&x);scanf("%c",&x);
int xx,yy;
scanf("%d%d",&xx,&yy);cost[x-'a']=min(xx,yy);
}
memset(dp,/,sizeof(dp));
for(int i=;i<=len;i++) dp[i][i]=,dp[i][i-]=;
for(int lenn=;lenn<len;lenn++){
for(int i=;i+lenn<=len;i++){
int j=i+lenn;
if(s[i]==s[j]) dp[i][j]=dp[i+][j-];
dp[i][j]=min(dp[i][j],min(dp[i+][j]+cost[s[i]-'a'],dp[i][j-]+cost[s[j]-'a']));
}
}
printf("%d",dp[][len]);
return ;
}

Cheapest Palindrome POJ - 3280的更多相关文章

  1. DP:Cheapest Palindrome(POJ 3280)

    价值最小回文字符串 题目大意:给你一个字符串,可以删除可以添加,并且每一次对一个字母的操作都带一个权,问你转成回文串最优操作数. 如果这一题我这样告诉你,你毫无疑问知道这一题是LD(Levenshti ...

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

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

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

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

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

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

  5. poj 3280(区间DP)

    Cheapest Palindrome Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7869   Accepted: 38 ...

  6. Cheapest Palindrome(区间DP)

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

  7. POJ3280 Cheapest Palindrome 【DP】

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

  8. poj 3280【区间dp】

    poj 3280 题意:给定一个字符串和每个字符删去和增加的代价,求使字符串变成回文串操作所需的最小代价. 题解:哇!开心!终于亲自做对了!做完这两题这个就回了.uva10739  uva 10453 ...

  9. poj 3280 Cheapest Palindrome

    链接:http://poj.org/problem?id=3280 思路:题目给出n种m个字符,每个字符都有对应的添加和删除的代价,求出构成最小回文串的代价 dp[i][j]代表区间i到区间j成为回文 ...

随机推荐

  1. 【Offer】[3-2] 【不修改数组找出重复的数字】

    题目描述 思路分析 Java代码 代码链接 题目描述 在一个长度为n+1的数组里的所有数字都在1~n的范围内,所以数组中至少有一个数字是重复的. 请找出数组中任意一个重复的数字,但不能修改输入的数组. ...

  2. 阿里云 windows frp 远程桌面

    环境: 阿里云服务器 server 2008 ,想要被远程访问的终端(本机)是win7 x64 目的:实现在别的地方(家里,出差在外) 用 远程桌面 访问 位于公司内部的电脑 frp 介绍:https ...

  3. 基于SSM的在线考试系统

    本系统功能非常完善,页面美观大方,技术新颖,选用主流数据库Mysql,表数量及结构适当,如果你需要做在线考试或者其它考试类系统,这个系统将非常有用. 其实,任何考试系统,无非试题不一样,所以如果你是做 ...

  4. 如何使用Java访问双向认证的Https资源

    本文的相关源码位于 https://github.com/dreamingodd/CA-generation-demo 0.Nginx配置Https双向认证 首先配置Https双向认证的服务器资源. ...

  5. Bootstrap4默认样式不对胃口?教你使用NPM+Webpack+SASS来定制

    Bootstrap 是一个流行的前端样式库,可以方便快速的构建应用,但默认样式可能不尽人意,本文就介绍如何使用 NPM, Webpack, SASS 针对它的源码来定制自己的主题.版本使用的是 Boo ...

  6. StateListDrawable资源的使用

    StateListDrawable用于组织多个Drawable对象,当使用StateListDrawable作为目标组件的 背景和前景图片时,StateListDrawable对象所显示的Drawab ...

  7. 【第十七篇】easyui-datagrid 导出Excel (在客户端能弹出下载框)

    //导出Excel function exportExcel(obj) { var SaleOrderNo = $("#SaleOrderNo").val().trim(); va ...

  8. JVM性能监视

    1,按照如下图步骤将-XX:PermSize及 -XX:MaxPermSize的值修改到足够小,故意造项目启动报错,我设置为32m.然后保存. 2.在IDE中启动tomcat的同时打开dos窗口,使用 ...

  9. iOS -打包上传成功,在"构建版本"一直刷不出来

    今天提交版本到appstore,构建版本一直不出来,等了一天也没有出来,其实就是权限问题,iOS13 来了,所以面临的问题随之而来,苹果给邮箱发了这段话: Dear Developer,We iden ...

  10. CSS——设置背景

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...