Test instructions
Give two strings of S1 and S2, only one interval can be brushed once, ask at least a few times to let S1=S2
For example, zzzzzfzzzzz, with a length of 11, we'll take the subscript as 0~10
First brush the 0~10 once, become aaaaaaaaaaa
1~9 Brush once, Abbbbbbbbba
2~8:abcccccccba
3~7:abcdddddcba
4~6:abcdeeedcab
5:abcdefedcab
That's 6 times, it's a S2 string.
1#include <cstdio>2#include <cstring>3#include <cmath>4#include <map>5#include <iostream>6#include <algorithm>7 using namespacestd;8 Const intmaxn= the;9 intDP[MAXN][MAXN];Ten CharA[MAXN],B[MAXN]; One intANS[MAXN]; A intMain () - { -Freopen ("1.in","R", stdin); the while(cin>>a>>b) - { - inti,j,k,n,m,p,q,len,t; -n=strlen (a); +Memset (DP,0,sizeof(DP)); - //for ease of handling, one needs to change in order to get the corresponding letter. + //because when a paragraph is updated, the values of the two strings that are originally equal are changed. A for(i=0; i<n;i++) dp[i][i]=1; at for(len=2; len<=n;len++)//Enumeration Length - { - for(i=0; i<n-len+1; i++)//enumeration starting point - { -j=i+len-1;//End - //cout<<i<< "" <<j<<endl; indp[i][j]=dp[i+1][j]+1;// !! Because only the comparison is b[i]=b[k], so can not be used dp[i][j]=dp[i][j-1]+1 - for(k=i+1; k<=j;k++)//Enumerate split points to { + if(b[i]==B[k]) - { the //cout<<i<< "" <<k<<endl; *Dp[i][j]=min (dp[i][j],dp[i+1][k]+dp[k+1][j]); $ }Panax Notoginseng } - } the } + for(i=0; i<n;i++) Aans[i]=dp[0][i]; the for(i=0; i<n;i++) + { - //recalculates the same point in two strings as the characters. $ if(a[i]==B[i]) $ { - if(i==0) ans[i]=0; - Elseans[i]=ans[i-1]; the } - ElseWuyi { the for(j=0; j<i;j++) -Ans[i]=min (ans[i],ans[j]+dp[j+1][i]); Wu } - } Aboutcout<<ans[n-1]<<Endl; $ } - return 0; -}
HDU 2476 interval DP