POJ 3280 DP

来源:互联网 发布:播放类软件 编辑:程序博客网 时间:2024/05/29 08:36

Cheapest Palindrome
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 7798 Accepted: 3780
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
Hint

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.

题意:

给出一个字符串,这个字符串的每一个不同的字母增加和删除都有一定的费用。问如何在费用最小的情况下通过增加删除把原串变成一个回文串。

题解:

首先我们只需要记录增加或者删除操作里小的那个,因为为了对称我们删除一边的字母和增加另一边的字母是一样的。
我们设dp[i][j]为字符串子串(i,j)变成回文串的最小花费。那么dp[i][j]要想是回文串就有两种可能:1,在(i+1,j)的基础上在i处的花费。2,在(i,j-1)的基础上加上在j处的花费.

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <stack>#include <string>#include <set>#include <cmath>#include <map>#include <queue>#include <sstream>#include <vector>#include <iomanip>#define m0(a) memset(a,0,sizeof(a))#define mm(a) memset(a,0x3f,sizeof(a))#define m_1(a) memset(a,-1,sizeof(a))#define f(i,a,b) for(i = a;i<=b;i++)#define fi(i,a,b) for(i = a;i>=b;i--)#define lowbit(a) ((a)&(-a))#define FFR freopen("data.in","r",stdin)#define FFW freopen("data.out","w",stdout)#define INF 0x3f3f3f3ftypedef long long ll;typedef long double ld;const ld PI = acos(-1.0);using namespace std;#define SIZE ( )char s[3000];int cost[30];int dp[3000][3000];int main(){    ios_base::sync_with_stdio(false); cin.tie(0);    int n,m;    while(cin>>n>>m){        cin>>s;        int i,j;        f(i,1,n){            char c;            int a,b;            cin>>c;            cin>>a>>b;            cost[c-'a'] = min(a,b);        }        m0(dp);        fi(i,m-1,0)            f(j,i+1,m-1){                if(s[i]==s[j])                    dp[i][j] = dp[i+1][j-1];                else                    dp[i][j] = min(dp[i+1][j]+cost[s[i]-'a'],dp[i][j-1]+cost[s[j]-'a']);            }        cout<<dp[0][m-1]<<endl;    }    return 0;}
0 0