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. iOS 枚举写法

    1.第一种 typedef enum { kPRStateNormal = 0, kPRStatePulling = 1, kPRStateLoading = 2, kPRStateHitTheEnd ...

  2. Struts2实现国际化

    public class I18nAction extends ActionSupport { private static final long serialVersionUID = -693330 ...

  3. gvim work notes.. a few days' work on 64bit vim and plugin compilations

    (a 600MB+ sized c/c++ compiler which is capable of hi-light and JB styled completion!! and of-course ...

  4. easyui easyui-filebox 显示中文

    <input class="easyui-filebox" name="uploadFile" id="uploadFileid" d ...

  5. 默认系统为UEFI启动的GPT分区的WIN7(8),如何安装VHD的UEFI WIN8(7)

    默认系统为UEFI启动的GPT分区的WIN7(8),如何安装VHD的UEFI WIN8(7) 情况A:如果默认系统为UEFI启动.GPT分区的WIN7,想安装个VHD的UEFI WIN8.1 1:系统 ...

  6. SimplePath 使用心得

    上图是 用SimplePath 做的 寻路,其中 三个 绿点 是 移动的 目标点,三个红点 是 角色移动,蓝色方块是阻挡物体. 这三个角色 移动 有三种 方式,1 随机移动2 按照 绿色小球点 移动  ...

  7. [HDU] 2094 产生冠军(拓扑排序+map)

    产生冠军 Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submissi ...

  8. Yii config 配置

    Yii2 配置文件 常用配置总结 <?php // 主配置文件 $config = array( 'modules' => array( 'gii' => array( 'class ...

  9. Codeforces Round #363 (Div. 2)D. Fix a Tree(并查集)

    D. Fix a Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  10. ab测试 uwsgi遇到的问题

    1 请求并发数目较大时,接收到的数目小于发送的数目 1.1 描述:uwsgi正常返回302跳转 ab -n 5000 -c 250 -g test.log "192.168.50.20:90 ...