区间染色的升级版
相当于两次区间DP
HDU 2476
//MADE BY Y_is_sunshine;
//#include <bits/stdc++.h>
//#include <memory.h>
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <cstdio>
#include <vector>
#include <string>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define INF 0x3f3f3f3f
#define MAXN 105
const int mod = 1e9 + 7;
const double PI = acos(-1);
using namespace std;
int dp[MAXN][MAXN];
int main()
{
freopen("data.txt", "r", stdin);
string s1, s2;
while (cin >> s1 >> s2) {
int N = s1.size();
s1.insert(s1.begin(), ' ');
s2.insert(s2.begin(), ' ');
for (int i = 1; i <= N; i++)
dp[i][i] = 1;
for (int len = 2; len <= N; len++) {
for (int i = 1; i + len - 1 <= N; i++) {
int j = i + len - 1;
dp[i][j] = INF;
if (s2[i] == s2[j])
dp[i][j] = dp[i][j - 1];
for (int k = i; k < j; k++)
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j]);
}
}
int ans[MAXN];
ans[0] = 0;
for (int i = 1; i <= N; i++)
ans[i] = dp[1][i];
for (int i = 1; i <= N; i++) {
if (s1[i] == s2[i])
ans[i] = ans[i - 1];
else
for (int j = 1; j < i; j++)
ans[i] = min(ans[i], ans[j] + dp[j + 1][i]);
}
cout << ans[N] << endl;
}
freopen("CON", "r", stdin);
system("pause");
return 0;
}