poj 3280 Cheapest Palindrome(区间dp)

来源:互联网 发布:暴风影音mac版能在线么 编辑:程序博客网 时间:2024/04/30 02:13

Language:
Cheapest Palindrome
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 6637 Accepted: 3219

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 4abcba 1000 1100b 350 700c 200 800

Sample Output

900


给出一个字符串 字符串里面每个字符都有两个值 代表删去这个字符和添加这个字符需要付出的代价

现在要经过一些操作使得这个字符串变为回文字符串  问最少付出多少代价

dp[i][j]表示区间i-j变为回文字符串的代价 

对于区间i-j有三种可能操作  如果i-(j-1)是回文串  则对于字符j有两种操作 删除和添加

如果(i+1)-j是回文串  则对于字符i有两种操作 删除和添加

如果(i+1)-(j-1)已经是回文串了 取这两个区间的最小值

#include <cstdio>#include <iostream>#include <cstring>#include <cmath>#include <algorithm>#include <string.h>#include <string>#include <vector>#include <queue>#define MEM(a,x) memset(a,x,sizeof a)#define eps 1e-8#define MOD 10009#define MAXN 10010#define MAXM 100010#define INF 99999999#define ll __int64#define bug cout<<"here"<<endl#define fread freopen("ceshi.txt","r",stdin)#define fwrite freopen("out.txt","w",stdout)using namespace std;int Read(){    char c = getchar();    while (c < '0' || c > '9') c = getchar();    int x = 0;    while (c >= '0' && c <= '9') {        x = x * 10 + c - '0';        c = getchar();    }    return x;}void Print(int a){     if(a>9)         Print(a/10);     putchar(a%10+'0');}char ch[2010];int add[30];int del[30];int dp[2010][2010];int main(){//    fread;    int n,m;    while(scanf("%d%d",&n,&m)!=EOF)    {        scanf("%s",ch);        getchar();        for(int i=0;i<n;i++)        {//            cout<<"n "<<n<<endl;            int ad,de;            char s;            scanf("%c%d%d",&s,&ad,&de);            getchar();            add[s-'a']=ad;            del[s-'a']=de;        }        MEM(dp,0);        for(int i=m-2;i>=0;i--)        {            for(int j=i+1;j<m;j++)            {                dp[i][j]=min(dp[i+1][j]+add[ch[i]-'a'],dp[i+1][j]+del[ch[i]-'a']);                dp[i][j]=min(dp[i][j],min(dp[i][j-1]+add[ch[j]-'a'],dp[i][j-1]+del[ch[j]-'a']));                if(ch[i]==ch[j])                {                    dp[i][j]=min(dp[i][j],dp[i+1][j-1]);                }            }        }        printf("%d\n",dp[0][m-1]);    }    return 0;}






0 0
原创粉丝点击