AcWing 1222. 密码脱落
原题链接
中等
作者:
回归线
,
2021-04-03 22:31:49
,
所有人可见
,
阅读 320
#include <iostream>
#include <string>
using namespace std;
const int N = 1010;
int n;
char s[N];
int f[N][N];
int main() {
string s;
cin >> s;
int n = s.size();
for (int len = 1; len <= n; ++len) {
for (int i = 0; i + len - 1 < n; ++i) {
int j = i + len - 1;
if (len == 1) {
f[i][j] = 1;
} else {
f[i][j] = max(f[i + 1][j], f[i][j - 1]);
if (s[i] == s[j]) {
f[i][j] = max(f[i][j], f[i + 1][j - 1] + 2);
}
}
}
}
cout << n - f[0][n - 1] << endl;
return 0;
}