# LA 4394 String painter

There 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 string painter. What's the minimum number of operations?

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define MAXN 2147483646
using namespace std;
int f[105],dp[105][105];
char a[105],b[105];
int main()
{
while(~scanf("%s",a+1))
{
scanf("%s",b+1);
int n = strlen(a+1);
memset(f,0,sizeof(f));
memset(dp,0,sizeof(dp));
for(int i = n;i;i--)
{
dp[i][i] = 1;
for(int j = i+1;j <= n;j++)
{
dp[i][j] = dp[i+1][j] + (b[i] == b[i+1] ? 0:1);
for(int k = i+1;k <= j;k++)
if(b[k] == b[i]) dp[i][j] = min(dp[i][j],dp[i+1][k]+dp[k+1][j]);
}
}
for(int i = 1;i <= n;i++)
if(b[i] == a[i]) f[i] = f[i-1];
else
{
f[i] = MAXN;
for(int j = i;j;j--)
if(b[j] == b[i]) f[i] = min(f[i],f[j-1]+dp[j][i]);
}
cout<<f[n]<<endl;
}
} 

#### LA4394

2013-01-09 17:02:07

#### String painter

2016-03-16 20:32:53

#### hdu 2476 String painter

2014-07-04 22:08:28

#### hdu String painter（dp）

2012-12-21 11:03:45

#### 【HDU2476】【String painter】

2015-08-05 00:07:42

#### hdu 2476 String painter(区间DP，较难)

2014-02-15 15:34:13

#### HDU2476：String painter(区间DP)

2013-08-02 00:36:52

#### hdu2476String painter (区间DP)

2014-07-20 12:31:38

#### hdu2476 String painter （区间DP）

2016-08-10 08:46:04

#### 区间DP小结(附经典例题)

2017-08-13 12:50:24