[区间dp]poj3280 Cheapest Palindrome

来源:互联网 发布:淄博市网络教研平台 编辑:程序博客网 时间:2024/04/30 00:07

Cheapest Palindrome

Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %lld & %llu
Submit

Status

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.
Source
USACO 2007 Open Gold

题目大意:
一个字符串,插入或删除一个字符都需要一定money,问这个字符串变成回文串的最小代价。。
dp[i][j],表示串i,j变成回文串的最小代价(突然感觉dp方程的含义有一种数学老师说的那种求什么设什么QAQ)
min(dp[i+1][j]+tmp1,dp[i][j-1]+tmp2)
tmp1为i字符右边添加i,或者左边减少i的最小值
tmp2同理
if s[i]==s[j],,min(dp[i+1][j-1],…)

注:可以看看dp[][]的初值,个人认为dp方程的初值很美丽。。。美丽。。。

代码:

#include<iostream>#include<cstdio>#include<ctime>#include<cstdlib>#include<cmath>#include<cstring>#include<string>#include<set>#include<map>#include<vector>#include<queue>#include<algorithm>#ifdef WIN32#define AUTO "%I64d"#else#define AUTO "%lld"#endif#define INF 0x3f3f3f3f#define clock CLOCKS_PER_SEC#define cle(x) memset(x,0,sizeof(x))#define maxcle(x) memset(x,127,sizeof(x))#define mincle(x) memset(x,-1,sizeof(x))#define minn(x1,x2,x3) min(x1,min(x2,x3))#define cop(a,x) memcpy(x,a,sizeof(a))#define FROP "poj"#define C(a,b) next_permutation(a,b)#define LL long long#define smin(x,tmp) x=min(x,tmp)using namespace std; const int N=2005,M=28;char s[N];int add[M],del[M],n,m,len;void init(){    scanf("%d%d",&n,&m);    scanf("%s",s+1);    len=strlen(s+1);    getchar();    for(int i = 1; i<= n; i++)    {        char ch;        scanf("%c",&ch);        int x,y;        scanf("%d%d",&x,&y);        add[ch-'a'+1]=x;        del[ch-'a'+1]=y;        getchar();    }}int dp[N][N];int main(){    freopen(FROP".in","r",stdin);    freopen(FROP".out","w",stdout);    init();    for(int k = 1; k<=len;k++)//我们可以知道方程是从i+1,j或者i,j-1,或者i+1,j-1,,总之位数都更小。        for(int i =1,j=k;i<=len&&j<=len;i++,j++)        {            dp[i][j]=INF;            int tmp1=min(add[s[i]-'a'+1],del[s[i]-'a'+1]);            int tmp2=min(add[s[j]-'a'+1],del[s[j]-'a'+1]);            dp[i][j]=min(dp[i+1][j]+tmp1,dp[i][j-1]+tmp2);            if(s[i]==s[j])dp[i][j]=min(dp[i][j],dp[i+1][j-1]);//一个字符,或者两个字符时,,i+1,j-1,并不会=INF,错位为0 ,所以初值不用设,,当有3个时,1个的已经为0了。。        }    printf("%d",dp[1][len]);    return 0;}
0 0
原创粉丝点击