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. ANT 操控 ORACLE数据库实践

    Ant 执行系统命令没有任何问题,这次实际系统命令中可以说遇到了两个问题,一个是启动服务的命令是含有空格的,第二个如何备份数据库可以自动加上日期. 首先,我们启动oracle数据库,操作有两个: 1. ...

  2. mysql 不能插入中文

    三.#vim /etc/mysql/my.cnf .(5.5以前系统)在[client]下面加入 default-character-set=utf8 在[mysqld]下面加入default-cha ...

  3. vs2013安装visual assist和viemu之后提示功能等无效解决

    1.vs2013安装了上面两个软件之后会发生va功能无效,经过一番谷歌百度后找到了解决方案 1.打开注册表 2.直接搜索TrackCaretVisibility这个键值,找到后把他的值修改成00 此篇 ...

  4. codeforces 689B Mike and Shortcuts 最短路

    题目大意:给出n个点,两点间的常规路为双向路,路长为两点之间的差的绝对值,第二行为捷径,捷径为单向路(第i个点到ai点),距离为1.问1到各个点之间的最短距离. 题目思路:SPFA求最短路 #incl ...

  5. hdu 5536 xor题

    input 1<=T<=1000 3<=n<=1000 s1 s2 ... sn 0<=si<=10e9 最多十个样例n>=100 output max((a ...

  6. Android中的分页加载

    //----------------------MainActivity中--------------------------------------------------- package com ...

  7. javascript语句语义大全(6)

    var d = new Date();//创建当前日期对象var d = new Date('2016/03/22');//允许var d = new Date('2016/3/22');//允许va ...

  8. HDU1896Stones(优先队列)

    Stones Time Limit : 5000/3000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other) Total Submis ...

  9. uploadify在IE6下的问题

    上传插件uploadify,在IE8下运行的没有问题.转到IE6下时,就不能上传了. 把浏览器的“检查所存网页的较新版本” 设置为“每次访问网页时 ”就没有问题,如果设置为“自动 ”,IE6下就不能上 ...

  10. 关于mysql 删除数据后物理空间未释放(转载)

    转自 关于mysql 删除数据后物理空间未释放(转载) - NETDATA - 博客园http://www.cnblogs.com/shawnloong/archive/2013/02/07/2908 ...