"Dynamic planning" poj3280-cheapest palindrome

Source: Internet
Author: User

"The main topic"

Gives a string that can delete or add some characters, each of which consumes value. Ask at least how much value, can make a string into a palindrome.

Ideas

In fact the value of deleting or adding characters only needs to keep the smaller one. Assuming that you are currently converting (j,i) to a literal, it has the following three scenarios:

(1) Add or remove at the end of a character that is the same as the beginning, f[j][i-1]+cost[s[i]-' a ';

(2) Adding or deleting a character at the beginning and ending the same, f[j+1][i]+cost[s[j]-' a ';

(3) If the beginning and end of the character is the same, there is f[j+1][i-1].

Error

Initialization is 0, do not set the INF.

1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 6 Const intmaxn= -+ -;7 intn,m;8 CharS[MAXN];9 intF[MAXN][MAXN];Ten intCOST[MAXN]; One  A intMain () - { -scanf"%d%d",&n,&m); the GetChar (); -scanf"%s", s); -Memset (F,0,sizeof(f)); -      for(intI=0; i<n;i++) +     { - GetChar (); +         CharTEMPC; A         intb; atscanf"%c%d%d",&tempc,&a,&b); -cost[tempc-'a']=min (A, b); -     } -  -      for(intI=1; i<m;i++) -          for(intj=i-1; j>=0; j--) in         { -F[j][i]=min (f[j+1][i]+cost[s[j]-'a'], f[j][i-1]+cost[s[i]-'a']); to             if(S[j]==s[i]) f[j][i]=min (f[j][i],f[j+1][i-1]); +         } -cout<<f[0][m-1]<<Endl; the     return 0; *}

"Dynamic planning" poj3280-cheapest palindrome

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.