Bzoj 1710: [Usaco2007 open]cheappal cheap palindrome "Interval DP"

Source: Internet
Author: User

As long as you find that adding a character and deleting one character is equivalent, it's a pretty naked interval dp.
Because adding a character x at the current position is equivalent to deleting the character X in his symmetrical position, so long as the deletion is considered, the deletion fee is the addition and deletion of the min
Set F[I][J] for the price from I to J, the length from small to large enumeration update on the line F[i][j]=min (F[i][j-1]+cost[s[j]],f[i+1][j]+cost[s[i]]), if S[I]==S[J] can and f[i+1][j-1 ] Take a min
cpp #include<iostream> #include<cstdio> using namespace std; const int N=2005; int m,n,a[30],f[N][N],x,y; char s[N],ch[10]; int main() { scanf("%d%d",&m,&n); scanf("%s",s+1); for(int i=1;i<=m;i++) { scanf("%s%d%d",ch,&x,&y); a[ch[0]-‘a‘]=min(x,y); } for(int i=1;i<=n;i++) s[i]-=‘a‘,f[i][i]=0,f[i+1][i]=0; for(int l=1;l<=n;l++) for(int i=1;i+l<=n;i++) { int j=i+l; f[i][j]=min(f[i][j-1]+a[s[j]],f[i+1][j]+a[s[i]]); if(s[i]==s[j]) f[i][j]=min(f[i][j],f[i+1][j-1]); } printf("%d\n",f[1][n]); return 0; } /* 3 4 abcb a 1000 1100 b 350 700 c 200 800 */

Bzoj 1710: [Usaco2007 open]cheappal cheap palindrome "Interval DP"

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.