Dynamic planning (Model conversion): Uvaoj 1625 Color Length

Source: Internet
Author: User

[PDF Link] topic point here

This problem is a dynamic plan, but it seems not good to do.

If the model is not converted, the state is difficult to handle.

Clever transformation: Do not directly to the letter and tail distance, but open to the state.

1#include <iostream>2#include <cstring>3#include <cstdio>4 using namespacestd;5 Const intmaxn=5010;6 CharS1[MAXN],S2[MAXN];7 intb1[ -],e1[ -],b2[ -],e2[ -];8 intDP[MAXN][MAXN];9 intADD[MAXN][MAXN];Ten intMain () { One     intT,len1,len2; Ascanf"%d",&T); -      while(t--){ -scanf"%s", s1+1); thescanf"%s", s2+1); -Len1=strlen (s1+1); -Len2=strlen (s2+1); -memset (B1,127,sizeof(B1)); +memset (e1,-1,sizeof(E1)); -memset (B2,127,sizeof(B2)); +memset (e2,-1,sizeof(E2)); A          for(intI=1; i<=len1;i++){ ats1[i]-='A'; -             if(B1[S1[I]]&GT;LEN1) b1[s1[i]]=i; -e1[s1[i]]=i; -         } -          -          for(intI=1; i<=len2;i++){ ins2[i]-='A'; -             if(B2[S2[I]]&GT;LEN2) b2[s2[i]]=i; toe2[s2[i]]=i; +         } -          the          for(intI=0; i<=len1;i++) *              for(intj=0; j<=len2;j++){ $                 if(i==j&&i==0)Continue;Panax Notoginseng                 if(!i| | j&&dp[i][j-1]+add[i][j-1]<=dp[i-1][j]+add[i-1][j]) { -dp[i][j]=dp[i][j-1]+add[i][j-1]; theadd[i][j]=add[i][j-1]; +                     if(b2[s2[j]]==j&&b1[s2[j]]>i) Aadd[i][j]++; the                     if(e2[s2[j]]==j&&e1[s2[j]]<=i) +add[i][j]--; -                 } $                 if(!j| | i&&dp[i][j-1]+add[i][j-1]>dp[i-1][j]+add[i-1][j]) { $dp[i][j]=dp[i-1][j]+add[i-1][j]; -add[i][j]=add[i-1][j]; -                     if(b1[s1[i]]==i&&b2[s1[i]]>j) theadd[i][j]++; -                     if(e1[s1[i]]==i&&e2[s1[i]]<=j)Wuyiadd[i][j]--; the                 } -             } Wuprintf"%d\n", Dp[len1][len2]);  -     } About     return 0; $}

Dynamic planning (Model conversion): Uvaoj 1625 Color Length

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.