String Painter
Time limit:5000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2520 Accepted Submission (s): 1134
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 INPUTZZZZZFZZZZZABCDEFEDCBAABABABABABABCDCDCDCDCDCD
Sample Output67
Source2008 Asia Regional Chengdu
Recommendlcy | We have carefully selected several similar problems for you:2480 2481 2478 2482 2474 The main idea: to give you two strings, each operation can turn a string of any paragraph into any of the same lowercase The minimum number of times the operation can become a B-string
#include <stdio.h> #include <string.h> #define MIN (A, b) (a>b?b:a) int Dp[110][110],ans[110];char str1[ 110],str2[110];int Main () {while (scanf ("%s%s", str1+1,str2+1)!=eof) {int i,j,k;int len=strlen (str1+1); memset (dp,0, sizeof (DP)); memset (ans,0,sizeof (ans)); for (i=1;i<=len;i++) dp[i][i]=1;for (i=len-1;i>=1;i--)//a string and B-string one are not equal { for (j=i+1;j<=len;j++) {dp[i][j]=dp[i+1][j]+1;for (k=i+1;k<=j;k++) {if (Str2[i]==str2[k]) {dp[i][j]=min (dp[i][ J],DP[I+1][K-1]+DP[K][J]);}}} for (i=1;i<=len;i++) {ans[i]=dp[1][i];if (Str1[i]==str2[i]) {ans[i]=ans[i-1];} Else{for (j=1;j<i;j++) {ans[i]=min (ans[i],ans[j]+dp[j+1][i]);}}} printf ("%d\n", Ans[len]);}}
Hdoj topic 2474 String painter (interval dp)