题解:
比较水的题目
dp[i][j]表示[i...j]最少改变几次变成回文字符串
那么有三种转移
dp[i][j] = dp[i+1][j-1] + s[i] != s[j]
dp[i][j] = dp[i+1][j] + 1(删除左边的字符,或者在右边添加一个字符与左边匹配)
dp[i][j] = dp[i][j-1] + 1(删除右边的字符,或者在左边添加一个字符与右边匹配)
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
char S[];
int dp[][];
int dfs(int i, int j){
if(i >= j) return ;
if(dp[i][j] < ) return dp[i][j];
dp[i][j] = min(dp[i][j], dfs(i+, j) + );
dp[i][j] = min(dp[i][j], dfs(i, j-) + );
dp[i][j] = min(dp[i][j], dfs(i+, j-) + (S[i] != S[j]));
return dp[i][j];
} int main()
{
while(cin>>S){
int n = strlen(S);
memset(dp, , sizeof(dp));
cout<<dfs(, n-)<<endl;
}
return ;
}