String painter
Time Limit: 5000/2000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 1529 accepted submission (s): 680
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 Input
zzzzzfzzzzzabcdefedcbaababababababcdcdcdcdcdcd
Sample output
67
Source2008 Asia Regional Chengdu
Question: There are two strings with the same length. Now there is a painter. You can replace all the letters in a section of the first string with the same letter (this letter can be any one) at a time ), ask how many operations can be performed at least to convert the first string S1 to the second string S2.
Idea: first change the blank string to S2, DP [I] [J]-change I ~ J indicates the number of steps required for the target string. DP [I] [J] = DP [I] [J-1] + 1; j single brush when s [J] = s [K, J can be used to fl the k together. DP [I] [J] = min (DP [I] [J], DP [I] [k] + dp [k + 1] [J-1]);
After processing, we need to see how many times S1 needs to be sprayed to S2. What are the advantages of S1 over blank strings? The reason is that S1 has the same characters as S2. These characters can be the same as S2 without being brushed. Record 1 ~ With RES [I ~ The number of times the second string is sprayed in the I interval. If the I position of the first string is the same as the I position of the second string, the I position does not need to be sprayed, res [I] = res [I-1], if not the same, you need to rely on ~ (I-1) range variables to split, Res [I] = min (RES [I], Res [J] + dp [J + 1] [I]).
Code:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#define maxn 105#define MAXN 100005#define mod 100000000#define INF 0x3f3f3f3f#define pi acos(-1.0)#define eps 1e-6typedef long long ll;using namespace std;int n,m,ans,cnt,tot;char s1[maxn],s2[maxn];int dp[maxn][maxn],res[maxn];int main(){ int i,j,t,k,len; while(~scanf("%s%s",s1+1,s2+1)) { memset(dp,0,sizeof(dp)); n=strlen(s1+1); for(i=1;i<=n;i++) { dp[i][i]=1; } for(len=2;len<=n;len++) { for(i=1;i<=n;i++) { j=i+len-1; if(j>n) break ; dp[i][j]=dp[i][j-1]+1; for(k=i;k<j;k++) { if(s2[k]==s2[j]) dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j-1]); } } } for(i=1;i<=n;i++) { res[i]=dp[1][i]; } for(i=1;i<=n;i++) { if(s1[i]==s2[i]) res[i]=res[i-1]; else { for(j=1;j<i;j++) { res[i]=min(res[i],res[j]+dp[j+1][i]); } } } printf("%d\n",res[n]); } return 0;}