hdu3280

来源:互联网 发布:长春java 编辑:程序博客网 时间:2024/06/08 13:37
Cheapest Palindrome
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 7248 Accepted: 3498

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

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.


关于这道题,一开始自己推,结果wr了,有很多盲点,没有想到。看了题解后,用题解的方法交了一遍ac,不死心,拉上了队友,又改了下自己的,结果ac了,虽然用时比较多!!!

根据这道题得出的显而易见的结论,所谓dp是要和题的特点结合起来的,比如说这道题是回文串,那么要和回文串的特点结合起来。不一定很死板的和别的题的dp入手一样。否则会事倍功半。

参考链接:http://www.cnblogs.com/ziyi--caolu/archive/2013/08/04/3236035.html

自己的方法,就是枚举字符串中第j+1个字母存在的方式:

1.删去

2.添一个新的相同的字母和它对应

3.和 i 到 j 中已经存在的某个和j+1相同的字母对应,。

4.最为已排好的回文串的中心。

自己的方法ac的代码:

#include<stdio.h>#include<string.h>#include<iostream>using namespace std;#define N 2100#define M 30char ch[N];int pay[M][2];int dp[N][N];int place[N][M];int sum[N];int main(){    int n,m;    char tempch;    int length;    while(scanf("%d%d",&n,&m)!=EOF){        scanf("%s",ch);        length=m;        for(int i=0;i<n;i++){            getchar();            scanf("%c",&tempch);            //printf("%c ",tempch);            scanf("%d%d",&pay[tempch-'a'][0],&pay[tempch-'a'][1]);        }        sum[0]=0;        for(int i=1;i<=length;i++){            sum[i]=sum[i-1]+min(pay[ch[i-1]-'a'][0],pay[ch[i-1]-'a'][1]);            //printf("%d %c %d %d %d\n",i,ch[i],ch[i]-'a',pay[ch[i]-'a'][0],sum[i]);        }        /*for(int i=0;i<length;i++){            printf("%d ",sum[i]);        }        printf("\n");*/        int cou=0;        for(int i=0;i<M;i++){            place[0][i]=-1;        }        for(int i=0;i<length;i++){            if(place[0][ch[i]-'a']==-1){                /*if(ch[i]-'a'==3){                    printf("%d ",i);                }*/                place[0][ch[i]-'a']=i;                cou++;                if(cou==n){                    break;                }            }        }        for(int i=1;i<length;i++){            for(int j=0;j<M;j++){                place[i][j]=place[i-1][j];            }            place[i][ch[i-1]-'a']=-1;            for(int j=i;j<length;j++){                if(ch[j]==ch[i-1]){                    place[i][ch[i-1]-'a']=j;                    break;                }            }        }        /*for(int i=0;i<length;i++){            for(int j=0;j<10;j++){                printf("%d ",place[i][j]);            }            printf("\n");        }*/        memset(dp,0,sizeof(dp));        for(int i=2;i<=length;i++){//少了情况,但是为什么会少情况呢?            for(int j=0;j<=length-i;j++){                dp[j][j+i-1]=dp[j][j+i-2]+min(pay[ch[j+i-1]-'a'][1],pay[ch[j+i-1]-'a'][0]);                int temp=place[j][ch[j+i-1]-'a'];                if(temp<j+i-1&&temp!=-1){                    if(temp!=j+i-2){                        dp[j][j+i-1]=min(dp[j][j+i-1],sum[temp]-sum[j]+dp[temp+1][j+i-2]);//这里不一定要加上某个字母,可以是删去,总之取最小值。                    }                    else{                        dp[j][j+i-1]=min(dp[j][j+i-1],sum[temp]-sum[j]);                    }                }                dp[j][j+i-1]=min(dp[j][j+i-1],sum[j+i-1]-sum[j]);//忽略了ch[j+i-1]可能是回文串的中心,不一定必须和另一个相同的字母对应起来。            }        }        /*for(int i=0;i<length;i++){            for(int j=0;j<length;j++){                printf("%d ",dp[i][j]);            }            printf("\n");        }*/        printf("%d\n",dp[0][length-1]);    }    return 0;}

根据题解ac的代码:
#include<stdio.h>#include<string.h>#include<iostream>using namespace std;#define N 2010#define M 30char ch[N];int dp[N][N];int pay[M][3];int main(){    int n,m;    char tempch;    while(scanf("%d%d",&n,&m)!=EOF){        scanf("%s",ch);        for(int i=0;i<n;i++){            getchar();            scanf("%c",&tempch);            scanf("%d%d",&pay[tempch-'a'][0],&pay[tempch-'a'][1]);            pay[tempch-'a'][2]=min(pay[tempch-'a'][0],pay[tempch-'a'][1]);        }        memset(dp,0,sizeof(dp));        for(int i=2;i<=m;i++){            for(int j=0;j<=m-i;j++){                dp[j][j+i-1]=min(dp[j+1][j+i-1]+pay[ch[j]-'a'][2],dp[j][j+i-2]+pay[ch[j+i-1]-'a'][2]);                if(ch[j]==ch[j+i-1]){                    if(j!=j+i-2){                        dp[j][j+i-1]=min(dp[j][j+i-1],dp[j+1][j+i-2]);                    }                    else{                        dp[j][j+i-1]=min(dp[j][j+i-1],0);                    }                }            }        }        /*for(int i=0;i<m;i++){            for(int j=0;j<m;j++){                printf("%d ",dp[i][j]);            }            printf("\n");        }*/        printf("%d\n",dp[0][m-1]);    }    return 0;}



0 0
原创粉丝点击