POJ1159(Palindrome)

题目链接

题目大意,给定一个字符串,求至少需插入多少字符使其变成回文。动态规划题。

#include <stdio.h>
#include <memory.h>
#include <string.h>
#define MIN(a,b) ((a)<(b)?(a):(b))
#define N 5001
char s[N];
int c[N];
int main()
{
int i,j,n,a,b;
while(scanf("%d",&n)!=EOF){
getchar();
gets(s);
for(i=n-2;i>=0;i--){
a=0;
for(j=i+1;j<n;j++){
b=a,a=c[j];
if(s[i]==s[j])
c[j]=b;
else
c[j]=MIN(c[j],c[j-1])+1;
}
}
printf("%d\n",c[n-1]);
memset(c,0,sizeof(c));
}
return 0;
}

 

posted @ 2012-04-07 20:11  BeatLJ  阅读(264)  评论(0编辑  收藏  举报