###### poj1159

short int

#include <iostream>
#include <cstdio>
#include <cstring>
#include <climits>
#include <algorithm>

using namespace std;
char a[5001],b[5001];
short int dp[5001][5001];
int n;

int solve()
{
memset(dp,0,sizeof(dp));
for(int i=n-1,j=0;i>=0;--i,++j)
b[j] = a[i];
for(int i=0;i<n;++i)
for(int j=0;j<n;++j)
if(*(a+i)==*(b+j))
dp[i+1][j+1]=dp[i][j]+1;
else
dp[i+1][j+1]=max(dp[i+1][j],dp[i][j+1]);
return n-dp[n][n];
}

int main()
{
while(scanf("%d",&n)!=EOF)
{
scanf("%s",a);
printf("%d\n",solve());
}

return 0;
}


roll

#include <iostream>
#include <cstdio>
#include <cstring>
#include <climits>
#include <algorithm>

using namespace std;
char a[5001],b[5001];
short int dp[2][5001];
int n;
int r1=0,r2=1,temp;
void cc()
{
memset(dp[r1],0,sizeof(dp[r1]));
temp=r1;
r1=r2;
r2=temp;
}

int solve()
{
memset(dp,0,sizeof(dp));
for(int i=n-1,j=0;i>=0;--i,++j)
b[j] = a[i];
for(int i=0;i<n;++i)
{
for(int j=0;j<n;++j)
if(*(a+i)==*(b+j))
dp[r2][j+1]=dp[r1][j]+1;
else
dp[r2][j+1]=max(dp[r2][j],dp[r1][j+1]);
cc();
}
return n-dp[r1][n];
}

int main()
{
while(scanf("%d",&n)!=EOF)
{
scanf("%s",a);
printf("%d\n",solve());
}

return 0;
}


#### POJ1159-Palindrome

2011-07-31 12:18:26

#### poj1159 Palindrome(最长公共子序列)

2014-08-23 21:48:20

#### poj1159（回文串）

2014-01-16 16:39:06

#### POJ1159

2014-07-14 10:12:58

#### poj1159解题报告

2010-08-11 20:25:00

#### POJ 1159 Palindrome 题解

2014-08-02 21:24:27

#### POJ1159,Palindrome

2014-08-05 09:17:35

#### poj1159（dp）

2015-08-19 20:00:56

#### poj1159 DP

2010-01-24 23:39:00

#### poj1159 - Palindrome

2012-06-05 20:44:16

poj1159