Problem Description
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?
Input
Input contains multiple cases. Each case consists of two lines:
The first line contains string A.
The second line contains string B.
The length of both strings will not be greater than 100.
Output
A single line contains one integer representing the answer.
Sample Input
zzzzzfzzzzz
abcdefedcba
abababababab
cdcdcdcdcdcd
Sample Output
6
7
这个题实在是太6了…
因为直接转移太难转移了…
所以题解是先算出来从一个空的直接转移到b串的dp
那么就直接+1就行了可以不用多bb
遇到两端点相同的区间就枚举一下所有子区间求最小的..
这样就相当于隐性的少了一个+1操作…
这tmd实在是太6了….
然后再去算从a怎么到b….
不一样的当空白算….一样的等于前一个…..
哇这个题太骚了
#include<bits/stdc++.h>
using namespace std;
int dp[101][101],dan[101];
string q,w;
int main()
{
while(cin>>q>>w)
{
int n=q.size();
memset(dp,0,sizeof(dp));
for(int a=0;a<n;a++)dp[a][a]=1;
for(int a=0;a<n;a++)
{
for(int b=a-1;b>=0;b--)
{
dp[b][a]=dp[b+1][a]+1;
for(int c=b+1;c<=a;c++)
{
if(w[b]==w[c])dp[b][a]=min(dp[b+1][c]+dp[c+1][a],dp[b][a]);
// dp[b][a]=min(dp[b][c]+dp[c+1][a],dp[b][a]);
}
}
}
for(int a=0;a<n;a++)
{
if(q[a]==w[a])
{
if(a==0)dan[a]=0;
else
{
dan[a]=dan[a-1];
}
continue;
}
dan[a]=dp[0][a];
for(int b=0;b<a;b++)
{
dan[a]=min(dan[a],dan[b]+dp[b+1][a]);
}
}
cout<<dan[n-1]<<endl;
}
}