Test instructions: Given two strings, let the minimum number of changes change from the first string to the second string
Idea: Interval DP, directly consider two strings of words too difficult, only consider the second string, to change from the blank string to the second string of the minimum number of times, dp[i][j] I->j This interval of the optimal solution, then dp[i][j] = min (dp[i + 1][j), Dp[i + 1][k] + Dp[k + 1][j]), this state transfer equation is the position of the enumeration K, provided that the second string of the first character must be equal to the K-character, because this is the optimal, otherwise, there is no need to brush the position of K. At the end of each position enumeration, see if the first string is equal to the corresponding position of the second string, and find the optimal solution.
#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intMAXN = the;intDP[MAXN][MAXN], ANS[MAXN];CharA[MAXN], B[MAXN];intMain () { while(~SCANF ("%s%s", A +1, B +1)) { intLen = strlen (A +1); Memset (DP,0,sizeof(DP)); for(inti = len; I >=1; i--) { for(intj = i; J <= Len; J + +) {Dp[i][j]= Dp[i +1][J] +1; for(intK = i +1; K <= J; k++) if(B[i] = =B[k]) dp[i][j]= Min (Dp[i][j], dp[i +1][k] + dp[k +1][j]); } } for(inti =1; I <= Len; i++) Ans[i]= dp[1][i]; for(inti =1; I <= Len; i++) { if(A[i] = =B[i]) ans[i]= Ans[i-1]; Else//if not, just enumerate each position and find a minimum to { for(intj =1; J < I; J + +) Ans[i]= Min (Ans[i], ans[j] + dp[j +1][i]); }} printf ("%d\n", Ans[len]); } return 0;}View Code
HDU 2476 String painter (interval dp)