POJ-3280
Cheapest Palindrome
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10301 Accepted: 4931 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.
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 800Sample Output
900Hint
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.
题意:
有一个长度为m的字符串,其中有n个不同的字母,每个字母有自己的添加和删除的花费,求如何增删字母,可以使字符串变为回文串且花费最小。
对于一个字母来说,增和删的效果是一样的,所以可将它的花费看做增删的最小值。
设dp[i][[j]为i~j区间内字母变为回文的最小花费,则它只由dp[i+1][j]+cost[i]和dp[i][j-1]+cost[j]决定。
当s[i]==s[j]时,dp[i][j]=dp[i+1][j-1]。则dp[i][j]为两者之间的最小值。
AC代码:
//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std; const int MAXN=; int dp[MAXN][MAXN];
int cost[]; int cmp(int a,int b){
if(a>b)
a=b;
return a;
} int main(){
ios::sync_with_stdio(false);
int n,m;
string s;
char c;
while(cin>>n>>m&&n&&m){
cin>>s;
int x,y;
for(int i=;i<n;i++){
cin>>c>>x>>y;
cost[c-'a']=min(x,y);
}
memset(dp,,sizeof(dp));
for(int i=m-;i>=;i--){
for(int j=i+;j<m;j++){
dp[i][j]=cmp(dp[i+][j]+cost[s[i]-'a'],dp[i][j-]+cost[s[j]-'a']);
if(s[i]==s[j]){
dp[i][j]=cmp(dp[i+][j-],dp[i][j]);
}
}
}
cout<<dp[][m-]<<endl;
}
return ;
}
POJ-3280的更多相关文章
- poj 3280【区间dp】
poj 3280 题意:给定一个字符串和每个字符删去和增加的代价,求使字符串变成回文串操作所需的最小代价. 题解:哇!开心!终于亲自做对了!做完这两题这个就回了.uva10739 uva 10453 ...
- poj 3280 Cheapest Palindrome
链接:http://poj.org/problem?id=3280 思路:题目给出n种m个字符,每个字符都有对应的添加和删除的代价,求出构成最小回文串的代价 dp[i][j]代表区间i到区间j成为回文 ...
- 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 思路: dp[i][j] :=第i个字符到第j个字符之间形成回文串的最小费用. dp[i][j]=min(dp[i+1][j]+ ...
- 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。
Description Keeping track of all the cows can be a tricky task so Farmer John has installed a system ...
- DP:Cheapest Palindrome(POJ 3280)
价值最小回文字符串 题目大意:给你一个字符串,可以删除可以添加,并且每一次对一个字母的操作都带一个权,问你转成回文串最优操作数. 如果这一题我这样告诉你,你毫无疑问知道这一题是LD(Levenshti ...
- POJ 3280 Cheapest Palindrome(DP)
题目链接 被以前的题目惯性思维了,此题dp[i][j],代表i到j这一段变成回文的最小花费.我觉得挺难的理解的. #include <cstdio> #include <cstrin ...
- POJ 3280 Cheapest Palindrome(DP)
题目链接 题意 :给你一个字符串,让你删除或添加某些字母让这个字符串变成回文串,删除或添加某个字母要付出相应的代价,问你变成回文所需要的最小的代价是多少. 思路 :DP[i][j]代表的是 i 到 j ...
随机推荐
- pulsar学习笔记1:helloworld
pulsar号称是下一代的消息系统,这二年风光无限,大有干掉kafka的势头,如果想快速体验下,可以按以下步骤在本地搭建一个单机版本:(mac环境+jdk8) 一. 下载 wget https://w ...
- Python中文编码过程中遇到的一些问题
首先,要明确encode()和decode()的差别 encode()的作用是将Unicode编码的字符串转换为其它编码格式. 比如:st1.encode("utf-8") 这句 ...
- 基于红帽5裁剪一个简单的Linux
HOST:宿主机 Target:目标机 1.基于HOST制作一个简单的可启动的Linux 1.给目标磁盘分区 两个: 在宿主机上:/dev/sdb1,/dev/sdb2 /dev/sdb1挂载到 /m ...
- php部分--头像上传预览
前台页面显示 <style type="text/css"> #yl{ width:200px; height:300px; background-image:url( ...
- COGS1817. [WC2013]糖果公园
1817. [WC2013]糖果公园 ★★★☆ 输入文件:park.in 输出文件:park.out 简单对比时间限制:8 s 内存限制:512 MB [题目描述] Candyland ...
- 【BZOJ4514】[Sdoi2016]数字配对 费用流
[BZOJ4514][Sdoi2016]数字配对 Description 有 n 种数字,第 i 种数字是 ai.有 bi 个,权值是 ci. 若两个数字 ai.aj 满足,ai 是 aj 的倍数,且 ...
- 可能是目前市面上唯一能够支持全平台的RTMP推流组件:Windows、Linux、Android、iOS、ARM
EasyRTMP是什么? EasyRTMP是一套RTMP直播推送功能组件,内部集成了包括:基本RTMP协议.断线重连.异步推送.环形缓冲区.推送网络拥塞自动丢帧.缓冲区关键帧检索.事件回调(断线.音视 ...
- 九度OJ 1150:Counterfeit Dollar(假美元) (分析、检验)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:485 解决:215 题目描述: Sally Jones has a dozen Voyageur silver dollars. Howev ...
- Hadoop实战-Flume之Source replicating(十四)
a1.sources = r1 a1.sinks = k1 k2 a1.channels = c1 c2 # Describe/configure the source a1.sources.r1.t ...
- 【题解】POJ1934 Trip (DP+记录方案)
[题解]POJ1934 Trip (DP+记录方案) 题意: 传送门 刚开始我是这么设状态的(谁叫我DP没学好) \(dp(i,j)\)表示钦定选择\(i\)和\(j\)的LCS,然而你会发现这样钦定 ...