POJ 3280:cheapest palindrome interval dp good problem

Source: Internet
Author: User

Cheapest Palindrome Topic Links:

http://poj.org/problem?id=3280

Test instructions

Given a string of only lowercase letters, you can add or delete some letters (both add and delete cost and vary) to change the string to the minimum cost of a palindrome string.

Exercises

Set DP[I][J] is the minimum cost of changing the interval [i,j] into a palindrome, then two cases

① obvious, when S[i]==s[j], dp[i][j]=dp[i+1][j-1]

② when S[i]!=s[j], Dp[i][j] has four possible values, interval [i,j-1] Delete s[i], add s[j-1 on the right of S[i], interval [i+1,j] Delete s[j], add s[i+1 on the left side of S[j], then dp[i][j]= Min (Dp[i][j-1]+del[i],dp[i][j-1]+add[i],dp[i+1][j]+del[j],dp[i+1][j]+add[j]);

Code
#include<stdio.h>#include<string.h>Constint N=2001;int DP[N][n],cost1[26],cost2[26];Char s[N],a[2];IntMmin(int x,int y){return x<y? x: Y;}voidSolve(){int nMBC;Memset(DP,0,sizeof(DP));While(~scanf("%d%d", &n, &m)){scanf('%s ', S+1);For(int I=1; I<=n; ++i){scanf("%s%d%d"A, &b, &c); Cost1[A[0]-A]=b; cost2[A[0]-A]=c;}For(int Len=1; Len<m; ++len){For(int I=1; I+len<=m; ++i){Int J=i+len;If(s[I]==s[j]) DP[I][j]=dp[I+1][j-1];Else{b=dp[I+1][j]+Mmin(Cost1[s[I]-A],cost2[s[I]-A]); c=dp[I][j-1]+Mmin(Cost1[s[j]-A],cost2[s[j]-A]);DP[I][j]=mmin (B,c}}} Printf ( "%d\n  ",dp[1][m}}int main< Span class= "Sh_symbol" > () {solve (); Span class= "Sh_keyword" >return 0;}   

POJ 3280:cheapest palindrome interval dp good problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.