PKU 1159 Palindrome

 PKU 1159 Palindrome  http://acm.pku.edu.cn/JudgeOnline/problem?id=1159

还是LCS,但数据改到了5000, 很容易超内存,要注意!

我用的是滑动数组的方法实现内存优化的:

 

#include <iostream>

using namespace std;

char a[5001];

int y[5001];

int temp[5001];

int main()

{

         short i,j,n;

         while (scanf("%d",&n)!=EOF)

         {

                   scanf("%s",a+1);

                   for (i=0;i<5001;i++)

                   {

                            y[i] = temp[i] = 0;

                   }

                   for (i=1;i<=n;i++)

                   {

                            for (j=1;j<=n;j++)

                            {

                                     if (a[i] == a[n-j+1]) //如果相等

                                     {

                                               if (j-1 == 0)

                                               {

                                                        temp[j] = 1;

                                               }

                                               else

                                                        temp[j] = y[j-1] + 1;

                                     }

                                     else // 如果不相等

                                     {

                                               if (j-1 == 0)

                                               {

                                                        temp[j] = y[j];

                                               }

                                               else

                                                        temp[j] = y[j] > temp[j-1] ?  y[j] : temp[j-1];

                                     }

                            }

                            for (j=1;j<=n;j++)

                            {

                                     y[j] = temp[j];

                                     temp[j] = 0;

                            }

                   }

                   printf("%d/n",n - y[n]);

         }

         return 0;

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值