# LA 4394 String painter

426人阅读 评论(0)

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;
}
} 

个人资料
等级：
访问量： 11万+
积分： 4588
排名： 7805
最新评论