//4_7_16: Palindrome 回文词的构造 POJ1159
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
char str1[5005];
char str2[5005];
short len[2][5005];
int i,j,N;
scanf("%d%s",&N,str1);
for(i = N - 1;i >= 0;i --)
str2[N - 1 - i] = str1[i];
str2[N] = '\0';
memset(len,0,sizeof(len));
for(i = 0;i < N;i ++)
{
for(j = 0;j < N;j ++)
{
if(str1[i] == str2[j]) len[1][j + 1] = len[0][j] + 1;
else len[1][j + 1] =
len[0][j + 1] > len[1][j] ? len[0][j + 1] : len[1][j];
}
for(j = 1;j <= N;j ++) //这里是滚动数组
len[0][j] = len[1][j];
}
printf("%d\n",N - len[1][N]);
return 0;
}
/*测试结果:通过POJ1159检测
5
Ab3bd
2
请按任意键继续. . .
*/
POJ1159 Palindrome
最新推荐文章于 2018-04-07 15:04:27 发布