[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]]>LEN1) b1[s1[i]]=i; -e1[s1[i]]=i; - } - - for(intI=1; i<=len2;i++){ ins2[i]-='A'; - if(B2[S2[I]]>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