String paintertime limit:2000msmemory limit:32768kbthis problem'll be judged onHDU. Original id:2476
64-bit integer IO format: %i64d Java class name: Main There 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
SourceRegional Asia Chengdu Problem Solving: Interval DP We can first assume the worst case, a brush a B, of course, when a is blank dp[i][j] means that the empty string I to J brush to B I to J need at least how many steps at this time have dp[i][j] = min (dp I [J],dp[i+1][k]+dp[k+1][j]) when b[i] = [B[k], why is dp[i+1][k]+dp[k+1][j] because when we brush the K at the same time I can be brushed off in the b[i] = = B[k] When the problem is a is not blank , then what should we do with ans[i] = min (Ans[i],ans[j]+dp[j+1][i]) to take j+1 to I as an empty string of brushes.
1#include <bits/stdc++.h>2 using namespacestd;3 Const intMAXN = -;4 intDP[MAXN][MAXN],ANS[MAXN];5 CharA[MAXN],B[MAXN];6 intMain () {7 while(~SCANF ("%s%s", B)) {8 intn =strlen (a);9Memset (DP,0,sizeofDP);Ten for(inti = n1; I >=0; --i) One for(intj = i; J < N; ++j) { ADP[I][J] = dp[i+1][J] +1; - for(intK = i+1; K <= J; ++k) { - if(B[i] = =B[k]) theDp[i][j] = min (dp[i][j],dp[i+1][k]+dp[k+1][j]); - } - } -memset (ans,0x3f,sizeofans); + for(inti =0; I < n; ++i) { - if(A[i] = = B[i]) ans[i] = i?ans[i-1]:0; + ElseAns[i] = dp[0][i]; A for(intj = I1; J >=0; --j) atAns[i] = min (ans[i],ans[j]+dp[j+1][i]); - } -printf"%d\n", ans[n-1]); - } - return 0; -}
View Code
HDU 2476 String Painter