[區間dp]poj3280 Cheapest Palindrome_dp

來源:互聯網
上載者:User
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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.