应该有更低的复杂度
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
#include <stack>
#include <cctype>
#include <utility>
#include <map>
#include <string>
#include <climits>
#include <set>
#include <string>
#include <sstream>
#include <utility>
#include <ctime>
using std::priority_queue;
using std::vector;
using std::swap;
using std::stack;
using std::sort;
using std::max;
using std::min;
using std::pair;
using std::map;
using std::string;
using std::cin;
using std::cout;
using std::set;
using std::queue;
using std::string;
using std::istringstream;
using std::make_pair;
using std::greater;
using std::endl;
const int MAXN(110);
const int INFI((INT_MAX-1) >> 1);
char str1[MAXN], str2[MAXN];
int table[MAXN][MAXN][27];
bool vis[MAXN][MAXN][27];
int dfs(int l, int r, int col)
{
if(l > r)
return 0;
if(vis[l][r][col])
return table[l][r][col];
int &cur = table[l][r][col];
if(col == str2[l] || (str1[l] == str2[l] && col == 27))
cur = dfs(l+1, r, col);
else
{
cur = INFI;
for(int i = l; i <= r; ++i)
cur = min(cur, dfs(l, i, str2[l])+dfs(i+1, r, col)+1);
}
vis[l][r][col] = true;
return cur;
}
int main()
{
while(~scanf("%s", str1+1))
{
scanf("%s", str2+1);
int len = strlen(str1+1);
for(int i = 1; i <= len; ++i)
{
str1[i] -= 'a';
str2[i] -= 'a';
}
memset(vis+1, 0, sizeof(vis[0])*len);
printf("%d\n", dfs(1, len, 27));
}
return 0;
}