当前位置: 代码迷 >> 综合 >> POJ-3280 Cheapest Palindrome DP 鶸的DP解题报告
  详细解决方案

POJ-3280 Cheapest Palindrome DP 鶸的DP解题报告

热度:15   发布时间:2023-12-12 14:15:53.0

题目:

Cheapest Palindrome
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 12011   Accepted: 5693

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 800

Sample Output

900

题意:给定一个字符串,同时给出它的所含字符总数和它所含字符的种类和对应的添加这个字符的代价,删除这个字符的代价。

求将字符串变为回文串的最小代价和。

思路:区间DP。设dp[i][j]将为区间i到j的字符变为回文字符串的最小代价。

个DP的思路是将已维护好回文的区间两边的边界不断扩大。然后在以维护好的区间上,找到扩大边界后区间的解。

则首先dp[i][i]为0。

之后若str[i]==str[j],dp[i][j] = dp[i+1][j-1];

若str[i]!=str[j] ,则分为两种情况。第一种情况是操作str[i]使这个区间变为回文串:可以选择直接删除str[i]或者在str[j]之后再添加一个str[i]。

int left = dp[i+1][i+l-1] + min(m[str[i]].in , m[str[i]].out);

第二种情况是操作str[j]使这个区间变为回文串:可以选择直接删除str[i]或者在str[i]之前再添加一个str[j]

int right = dp[i][i+l-2] + min(m[str[i+l-1]].in, m[str[i+l-1]].out);

使区间i到j变为回文串的最优解就是四种方法花费最小的解。

dp[i][i+l-1] = min(left,right);

当然。若要求出大区间的解,必须先求出小区间的解。所以写循环时可以先从区间长度为2开始,不断增加,直到找到整个字符串区间的解。

代码:

#include<cstdio>  
#include<cstdlib>  
#include<cmath>  
#include<cstring>  
#include<iostream>  
#include<algorithm>  
#include<queue>  
#include<map>  
#include<stack> 
#include<set>
#define sd(x) scanf("%d",&x)
#define ss(x) scanf("%s",x)
#define sc(x) scanf("%c",&x)
#define sf(x) scanf("%f",&x)
#define slf(x) scanf("%lf",&x)
#define slld(x) scanf("%lld",&x)
#define me(x,b) memset(x,b,sizeof(x))
#define pd(d) printf("%d\n",d);
#define plld(d) printf("%lld\n",d);
#define eps 1.0E-8
// #define Reast1nPeacetypedef long long ll;using namespace std;const int INF = 0x3f3f3f3f;int n,len;
string str;struct node{int in;int out;
}m[200];int dp[2010][2010];int main(){
#ifdef Reast1nPeacefreopen("in.txt", "r", stdin);freopen("out.txt", "w", stdout);
#endifios::sync_with_stdio(false);cin>>n>>len;cin>>str;for(int i = 1 ; i<=n ; i++){char c ; int in,out;cin>>c>>in>>out;m[c].in = in , m[c].out = out;}for(int l = 2 ; l<=len ; l++){for(int i = 0 ; i+l-1<len ; i++){if(str[i] == str[i+l-1]){dp[i][i+l-1] = dp[i+1][i+l-2];}else{int left = dp[i+1][i+l-1] + min(m[str[i]].in , m[str[i]].out);int right = dp[i][i+l-2] + min(m[str[i+l-1]].in, m[str[i+l-1]].out);dp[i][i+l-1] = min(left,right);}}}cout<<dp[0][len-1]<<endl;return 0;
}
  相关解决方案