Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=2476
Problem Descriptionthere is strings A and B with equal length. Both strings is made up of lower case letters. Now you have a powerful string painter. With the "help of the painter", you can change a segment of characters of a string of all other character you want. That's, after using the painter, the segment are made up for only one kind of character. Now the your task is to the change A to B using string painter. What ' s the minimum number of operations?
Inputinput contains multiple cases. Each case consists of lines:
The first line contains string A.
The second line contains string B.
The length of both strings would not be greater than 100.
Outputa single line contains one integer representing the answer.
Sample Input
Zzzzzfzzzzzabcdefedcbaababababababcdcdcdcdcdcd
Sample Output
67
Source2008 Asia Regional Chengdu
Test instructions
Given two strings, the first one is the initial string, the second is the target string, and asks you how many strings you need to change the initial string to the target string at a minimum!
Ps:
1: Assuming that the initial string is an empty string, then the interval dp,dp[i][j] indicates the interval [I, j] to the same minimum number of steps as required by the target string, there are: dp[i][j]=dp[i+1][j]+1;
2: if s2[i] = = S2[k], there: dp[i][j] = min (Dp[i][j], dp[i+1][k]+dp[k+1][j]).
The code is as follows:
#include <cstdio> #include <cstring> #define MAXN 117int MIN (int a, int b) {if (a < b) return A; return b;} int main () {int dp[maxn][maxn];//interval [I, j] the minimum number of times int A[MAXN]; Char S1[MAXN], S2[MAXN]; while (~SCANF ("%s", S1)) {scanf ("%s", S2); int len = strlen (S1); Memset (Dp,0,sizeof (DP)); The minimum number of intervals [i,j] is obtained; for (int j = 0; J < Len; J + +) {for (int i = j; I >= 0; i--) {Dp[i][j] = dp[i+1][j]+1;//one brush for (int k = i+1; k <= J; k++) {if (s2[i] = = S2[k ] {Dp[i][j] = MIN (Dp[i][j], dp[i+1][k]+dp[k+1][j]);//Optimal solution} }} A[j] = Dp[0][j]; } for (int i = 0; i < len; i++) {if (S1[i]==s2[i]) {a[i] = a[i-1]; If the corresponding position is equal, can not brush continue; } for (int j = 0; J < i; J + +) {A[i] = MIN (A[i],a[j]+dp[j+1][i]);//Best Solution}} printf ("%d\n", a[len-1]); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 2476 String painter (interval dp AH)