(中等) POJ 3280 Cheapest Palindrome,DP。
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。的更多相关文章
- poj 3280 Cheapest Palindrome ---(DP 回文串)
题目链接:http://poj.org/problem?id=3280 思路: dp[i][j] :=第i个字符到第j个字符之间形成回文串的最小费用. dp[i][j]=min(dp[i+1][j]+ ...
- POJ 3280 Cheapest Palindrome DP题解
看到Palindrome的题目.首先想到的应该是中心问题,然后从中心出发,思考怎样解决. DP问题通常是从更加小的问题转化到更加大的问题.然后是从地往上 bottom up地计算答案的. 能得出状态转 ...
- POJ 3280 Cheapest Palindrome(DP)
题目链接 被以前的题目惯性思维了,此题dp[i][j],代表i到j这一段变成回文的最小花费.我觉得挺难的理解的. #include <cstdio> #include <cstrin ...
- POJ 3280 Cheapest Palindrome(DP 回文变形)
题目链接:http://poj.org/problem?id=3280 题目大意:给定一个字符串,可以删除增加,每个操作都有代价,求出将字符串转换成回文串的最小代价 Sample Input 3 4 ...
- POJ 3280 - Cheapest Palindrome - [区间DP]
题目链接:http://poj.org/problem?id=3280 Time Limit: 2000MS Memory Limit: 65536K Description Keeping trac ...
- POJ 3280 Cheapest Palindrome(区间DP求改成回文串的最小花费)
题目链接:http://poj.org/problem?id=3280 题目大意:给你一个字符串,你可以删除或者增加任意字符,对应有相应的花费,让你通过这些操作使得字符串变为回文串,求最小花费.解题思 ...
- POJ 3280 Cheapest Palindrome(DP)
题目链接 题意 :给你一个字符串,让你删除或添加某些字母让这个字符串变成回文串,删除或添加某个字母要付出相应的代价,问你变成回文所需要的最小的代价是多少. 思路 :DP[i][j]代表的是 i 到 j ...
- POJ 3280 Cheapest Palindrome 简单DP
观察题目我们可以知道,实际上对于一个字母,你在串中删除或者添加本质上一样的,因为既然你添加是为了让其对称,说明有一个孤立的字母没有配对的,也就可以删掉,也能满足对称. 故两种操作看成一种,只需要保留花 ...
- POJ 3280 Cheapest Palindrome (DP)
Description Keeping track of all the cows can be a tricky task so Farmer John has installed a sys ...
随机推荐
- seo优化 标点符号
一.顿号“.” 顿号是一个只有在中文中使用的标点符号,这在英文中没有.毕竟该不该在标题中使用顿号呢,建议大家仍是不要使用,或者说在标题中就不要泛起中文的符号最好.不外,顿号可以使用在Descripti ...
- pom 的scope标签分析
一.compile:编译范围compile是默认的范围:如果没有提供一个范围,编译范围依赖在所有的classpath 中可用,同时它们也会被打包.而且这些dependency会传递到依赖的项目中. 二 ...
- cddiv/数组维护
题目连接 看代码: #include <set> #include <map> #include <cmath> #include <queue> #i ...
- gSoap工具wsdl2h及soapcpp2指令汇总
gSoap开发包的下载地址http://sourceforge.net/projects/gsoap2,在bin目录下提供了两个工具: 1:wsdl2h:The gSOAP wsdl2h tool i ...
- debian 安装 android studio 环境
jdk环境变量配置: ~/.hashrc export JAVA_HOME=/usr/share/jdk1.8.0_92 export PATH=$JAVA_HOME/bin:$PATH export ...
- angularJS Directive学习
Directive 指令 直接上实例 index.html <!doctype html> <html ng-app="drag"> <head> ...
- Apache2.4 137行 httpd-ahssl.conf
C:\Users\Administrator>E:\PHP\Apache24\bin\httpd.exe -w -n "apache2.4" -k startAH00526: ...
- angularjs ng-switch
<p> <a href="#" ng-click="toggle()">Toggle Section</a> </p& ...
- listview去掉条目间的分割线
未去掉前: 去掉后: java代码可以这么写: 1 listView.setDivider(null);//去掉条目间的分割线 PS:ListView的几个常用操作 listView ...
- 修改maven本地仓库路径
修改maven配置文件conf/settings.xml 在setting标签中添加 <localRepository>E:/bhuwifi_java/repo</localRepo ...