#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
using namespace std;
#define maxn 3005
int ans[maxn][maxn];
int longestCommonSubsequence(string &A, string &B)
{
int m = A.size();
int n = B.size();
for(int i = 0; i <= m; ++i)
{
ans[i][0] = 0;
}
for(int i = 0; i <= n; ++i)
{
ans[0][i] = 0;
}
for(int i = 1; i <= m; ++i)
{
for(int j = 1; j <= n; ++j)
{
if(A[i-1] == B[j-1])
{
ans[i][j] = ans[i-1][j-1] + 1;
}
else
{
ans[i][j] = max(ans[i-1][j], ans[i][j-1]);
}
}
}
return ans[m][n];
}
int main()
{
//freopen("data.in","r",stdin);
ios::sync_with_stdio(false);
string A;
string B;
cin >> A;
B = A;
reverse(B.begin(), B.end());
int res = longestCommonSubsequence(A, B);
cout << A.length() - res << endl;
return 0;
}
计蒜客 回文串(DP)
猜你喜欢
转载自blog.csdn.net/ccshijtgc/article/details/80894339
今日推荐
周排行