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.

  题目就是让把一个字符串变成一个回文串,可以任意删加,求最小代价。

  典型的DP问题,对于这个题可以联想到那个经典的题目,就是把两个字符串变成完全相同的最小代价,这个的话枚举向左和向右的两个字符串,然后一次求出变成相同的最小代价就好了。

  dp[i][j]表示i向左的那个串和j向右的那个串变成完全相同的代价。

代码如下:

// ━━━━━━神兽出没━━━━━━
// ┏┓ ┏┓
// ┏┛┻━━━━━━━┛┻┓
// ┃ ┃
// ┃ ━ ┃
// ████━████ ┃
// ┃ ┃
// ┃ ┻ ┃
// ┃ ┃
// ┗━┓ ┏━┛
// ┃ ┃
// ┃ ┃
// ┃ ┗━━━┓
// ┃ ┣┓
// ┃ ┏┛
// ┗┓┓┏━━━━━┳┓┏┛
// ┃┫┫ ┃┫┫
// ┗┻┛ ┗┻┛
//
// ━━━━━━感觉萌萌哒━━━━━━ // Author : WhyWhy
// Created Time : 2015年07月19日 星期日 16时29分25秒
// File Name : 3280.cpp #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h> using namespace std; const int MaxN=;
const long long INF=1000000000000000LL; int N,M;
char s[MaxN];
long long dp[MaxN][MaxN];
long long aC[MaxN],dC[MaxN]; int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); int a,b;
char ts[];
long long sum1,sum2; while(~scanf("%d %d",&N,&M))
{
scanf("%s",s);
memset(aC,,sizeof(aC));
memset(dC,,sizeof(dC)); for(int i=;i<=N;++i)
{
scanf("%s %d %d",ts,&a,&b);
aC[ts[]-'a']=a;
dC[ts[]-'a']=b;
} sum1=sum2=;
dp[][M]=; for(int j=M-;j>=;--j)
{
sum1+=aC[s[j]-'a'];
sum2+=dC[s[j]-'a'];
dp[][j]=min(sum1,sum2);
} sum1=sum2=; for(int i=;i<=M;++i)
{
sum1+=aC[s[i-]-'a'];
sum2+=dC[s[i-]-'a'];
dp[i][M]=min(sum1,sum2);
} for(int i=;i<=M;++i)
for(int j=M-;j>=i;--j)
if(s[i-]==s[j])
dp[i][j]=dp[i-][j+];
else
dp[i][j]=min(dp[i-][j]+min(aC[s[i-]-'a'],dC[s[i-]-'a']),dp[i][j+]+min(aC[s[j]-'a'],dC[s[j]-'a'])); long long minn=INF; for(int i=;i<=M;++i)
minn=min(minn,dp[i][i]); for(int i=;i<M;++i)
minn=min(minn,dp[i][i+]); printf("%lld\n",minn);
} return ;
}

(中等) POJ 3280 Cheapest Palindrome,DP。的更多相关文章

  1. poj 3280 Cheapest Palindrome ---(DP 回文串)

    题目链接:http://poj.org/problem?id=3280 思路: dp[i][j] :=第i个字符到第j个字符之间形成回文串的最小费用. dp[i][j]=min(dp[i+1][j]+ ...

  2. POJ 3280 Cheapest Palindrome DP题解

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

  3. POJ 3280 Cheapest Palindrome(DP)

    题目链接 被以前的题目惯性思维了,此题dp[i][j],代表i到j这一段变成回文的最小花费.我觉得挺难的理解的. #include <cstdio> #include <cstrin ...

  4. POJ 3280 Cheapest Palindrome(DP 回文变形)

    题目链接:http://poj.org/problem?id=3280 题目大意:给定一个字符串,可以删除增加,每个操作都有代价,求出将字符串转换成回文串的最小代价 Sample Input 3 4 ...

  5. POJ 3280 - Cheapest Palindrome - [区间DP]

    题目链接:http://poj.org/problem?id=3280 Time Limit: 2000MS Memory Limit: 65536K Description Keeping trac ...

  6. POJ 3280 Cheapest Palindrome(区间DP求改成回文串的最小花费)

    题目链接:http://poj.org/problem?id=3280 题目大意:给你一个字符串,你可以删除或者增加任意字符,对应有相应的花费,让你通过这些操作使得字符串变为回文串,求最小花费.解题思 ...

  7. POJ 3280 Cheapest Palindrome(DP)

    题目链接 题意 :给你一个字符串,让你删除或添加某些字母让这个字符串变成回文串,删除或添加某个字母要付出相应的代价,问你变成回文所需要的最小的代价是多少. 思路 :DP[i][j]代表的是 i 到 j ...

  8. POJ 3280 Cheapest Palindrome 简单DP

    观察题目我们可以知道,实际上对于一个字母,你在串中删除或者添加本质上一样的,因为既然你添加是为了让其对称,说明有一个孤立的字母没有配对的,也就可以删掉,也能满足对称. 故两种操作看成一种,只需要保留花 ...

  9. POJ 3280 Cheapest Palindrome (DP)

     Description Keeping track of all the cows can be a tricky task so Farmer John has installed a sys ...

随机推荐

  1. sql server 的datediff函数

    这两天要把一个sqlserver数据库的程序改成oracle的,发现两个数据库之间的函数很多不一样.sqlserver的数据库中的DateDiff 函数用法解释如下: 描述 返回两个日期之间的时间间隔 ...

  2. 【dp】 比较经典的dp poj 1160

    转自http://blog.sina.com.cn/s/blog_5dd8fece0100rq7d.html [题目大意]:用数轴描述一条高速公路,有V个村庄,每一个村庄坐落在数轴的某个点上,需要选择 ...

  3. Lua基础(一)

    1.Lua中有8个基本类型分别为:nil.boolean.number.string.userdata.function.thread和table 2.Lua中的表达式包括数字常量.字符串常量.变量. ...

  4. idea控制台输出乱码

    找到安装目录bin下面的idea64.exe.vmoptions,打开后在最后一行增加 -Xms128m -Xmx750m -XX:MaxPermSize=350m -XX:ReservedCodeC ...

  5. jquery的校验规则的方法

    //validate 选项*********************************************************** $("form").validat ...

  6. encodeURIComponent与URLDecoder.decode用法

    在输入地址栏时有时一些信息需要在地址栏看不见,我们就需要对其信息在前台转码后台解码 js:encodeURIComponent编码与解码 今天在js往jsp和servlet传参的时候出现:JavaSc ...

  7. Video Pooling

    Video pooling computes video representation over the whole video by pooling all the descriptors from ...

  8. stray '/241' in program 错误

    意思是c/c++中的编译错误. 该错误是指源程序中有非法字符,需要去掉非法字符.一般是由于从别的地方粘贴过来造成的. 方法:1.把所粘的文字放到记事本里就行了 2.把出错行的空格删掉重新打一下试试.

  9. Windows API 之 CreateToolhelp32Snapshot

    CreateToolhelp32Snapshot: 参考: https://msdn.microsoft.com/en-us/library/ms682489%28VS.85%29.aspx HAND ...

  10. 一个有意思的 hta 程序 (html application)

    哈哈,刚才同事给我讲了一个hta 程序,他自己说最近在学html5 开发坦克大战,不错,这种好奇心, 好学的精神值得我这个程序员学习,感觉他的视野面比我这个程序员还广,有点小惭愧. 什么是hta 呢? ...