HDU 2476 String painter, hdu2476
String painter
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 3639 Accepted Submission (s): 1697
Problem DescriptionThere are two strings A and B with equal length. both strings are 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 to any other character you want. that is, after using the painter, the segment is made up of only one kind of character. now your task is to change A to B using str Ing painter. What's the minimum number of operations?
InputInput contains multiple cases. Each case consists of two lines:
The first line contains string.
The second line contains string B.
The length of both strings will not be greater than 100.
OutputA single line contains one integer representing the answer.
Sample inputzzzzzfzzzzzabcdefedcbaabababababcdcdcdcd
Sample Output67
Source 2008 Asia Regional Chengdu
Recommendlcy: Given two equal-length strings, there is a method to refresh the string, which can fl a string into the same character (any character ). Now you need to use this method to make the first string be flushed into the second string. How many times do you need to fl at least? Solution: Obviously, this is a problem of interval DP. Set dp [I] [j] to the minimum number of times the interval [I, j] needs to be refreshed. It is difficult to determine the State Transfer Equation directly, so we need to consider directly flushing an empty string into a second string and then comparing it with the first string. In this way, if each character is refreshed separately, dp [I] [j] = dp [I + 1] [j] + 1. If [I + 1, j] is the same as t [I], and can be divided into two intervals: [I + 1, k] and [k + 1, j]. consider refresh together. For details, see the code. Attach the AC code: 1 # include <bits/stdc ++. h> 2 using namespace std; 3 const int maxn = 105; 4 char s [maxn], t [maxn]; 5 int dp [maxn] [maxn]; 6 7 int main () {8 while (~ Scanf ("% s", s, t) {9 memset (dp, 0, sizeof (dp); 10 int len = strlen (s ); 11 for (int j = 0; j <len; ++ j) 12 for (int I = j; I> = 0; -- I) {13 dp [I] [j] = dp [I + 1] [j] + 1; // each node is flushed separately for 14 for (int k = I + 1; k <= j; ++ k) 15 if (t [I] = t [k]) // The same color exists in the interval, consider flushing 16 dp [I] [j] = min (dp [I] [j], dp [I + 1] [k] + dp [k + 1] [j]); 17} 18 for (int I = 0; I <len; ++ I) {19 if (s [I] = t [I]) {// the corresponding location is the same, you can skip 20 if (I) 21 dp [0] [I] = dp [0] [I-1]; 22 else23 dp [0] [I] = 0; 24} 25 else26 for (int j = 0; j <I; ++ j) // find the optimal solution of the current interval 27 dp [0] [I] = min (dp [0] [I], dp [0] [j] + dp [j + 1] [I]); 28} 29 printf ("% d \ n", dp [0] [len-1]); 30} 31 return 0; 32}View Code