Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2476
First, it is converted from a blank string to string 2, DP processing. DP [I] [J] indicates the minimum operand between I and j. Then, the minimum operand from string 1 to string 2 is calculated.
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 6 char s1[110],s2[110]; 7 int dp[110][110]; 8 int c[110]; 9 10 int main()11 {12 while(scanf("%s%s",s1,s2)!=EOF)13 {14 int n=strlen(s1);15 for(int i=0; i<n; i++)16 {17 for(int j=i; j<n; j++)18 {19 dp[i][j]=j-i+1;20 }21 }22 for(int i=n-2; i>=0; i--)23 {24 for(int j=i+1; j<n; j++)25 {26 dp[i][j]=dp[i+1][j]+1;27 for(int k=i+1; k<=j; k++)28 {29 if(s2[i]==s2[k])30 dp[i][j]=min(dp[i][j],dp[i+1][k-1]+dp[k][j]);31 }32 }33 }34 for(int i=0; i<n; i++)35 {36 c[i]=dp[0][i];37 if(s1[i]==s2[i])38 {39 if(i==0) c[i]=0;40 else c[i]=c[i-1];41 }42 for(int j=0; j<i; j++)43 {44 c[i]=min(c[i],c[j]+dp[j+1][i]);45 }46 }47 printf("%d\n",c[n-1]);48 }49 return 0;50 }View code
HDU 2476 string painter