rt,用的是 wei_xin 大佬在这个贴 发的思路:
#include <iostream>
using namespace std;
const int N = 5010;
int T;
int n, x, y;
int f[N][N];
int k[N];
int cost(int l, int r) {
return min((k[r] - k[l]) * x, y);
}
int main() {
cin >> T;
while (T--) {
cin >> n >> x >> y;
string a, b; getline(cin, a);
getline(cin, a), getline(cin, b);
int cnt = 0;
for (int i = 0; i < n; i++)
if (a[i] != b[i]) k[++cnt] = i;
if (cnt % 2) {
cout << -1 << endl;
continue;
}
if (cnt == 2) {
bool flag = false;
for (int i = 0; i < n - 1; i++)
if (a[i] != b[i] && a[i + 1] != b[i + 1]) {
flag = true;
break;
}
if (flag) {
if (n == 2) cout << x << endl;
else cout << min(x, 2 * y) << endl;
continue;
}
cout << min(y, (k[2] - k[1]) * x) << endl;
continue;
}//cnt = 2 时候的特判
for (int i = 1; i < cnt; i++)
for (int j = i + 1; j <= cnt; j += 2)
if (k[i] + 1 != k[j]) f[i][j] = min((k[j] - k[i]) * x, y);
else f[i][j] = min(x, y * 2);//初始化
for (int len = 4; len <= n; len++) {
for (int l = 1; l < n; l++) {
int r = l + len - 1;
if (r > n) break;
f[l][r] = min(min(f[l + 2][r] + cost(l, l + 1), f[l][r - 2] + cost(r - 1, r)), f[l + 1][r - 1] + y/*cost(l, r)*/);//dp
}
}
cout << f[1][cnt] << endl;
}
return 0;
}