题目连接:
http://acm.hdu.edu.cn/showproblem.php?pid=1513
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 5005
int main()
{
int n,dp[2][N];
char a[N],b[N];
while(~scanf("%d",&n))
{
scanf("%s",a);
strcpy(b,a);//复制
strrev(b);//反转字符串
int len=strlen(b);
memset(dp,0,sizeof(dp));
int now,pre;
for(int i=1;i<=len;i++)
for(int j=1;j<=len;j++)
{
now=i%2;
pre=1-now;
if(a[i-1]==b[j-1])
dp[now][j]=dp[pre][j-1]+1;
else
dp[now][j]=dp[now][j-1]>dp[pre][j]?dp[now][j-1]:dp[pre][j];
}
printf("%d\n",len-dp[len%2][len]);
}
return 0;
}