
cpp
#include <bits/stdc++.h>
using namespace std;
int f[1005][1005];
int main() {
string s;cin>>s;
int n=s.size();
for(int i=0;i<n;i++){
f[i][i]=1;
}
for(int i=n-2;i>=0;i--){ // 从倒数第二个字符开始
for(int j=i+1;j<n;j++){ // 到字符串末尾
if(s[i]==s[j]){ // 如果两端字符相同,则回文子序列长度是中间部分+2
f[i][j]=f[i+1][j-1]+2;
} else{ // 如果两端字符不同,则取去掉左端或去掉右端的最大值
f[i][j] =max(f[i+1][j],f[i][j-1]);
}
}
}
cout<<n-f[0][n-1];
return 0;
}
f[i][j] 表示子串 s[i..j] 的最长回文子序列长度