poj 1159 Palindrome 【LCS】

时间:2024-04-10 10:35:40

任意门:http://poj.org/problem?id=1159

解题思路:

LCS + 滚动数组

AC code:

#include <cstdio>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int MAXN = 5e3+; char a[MAXN], b[MAXN];
int dp[][MAXN]; int main()
{
int len;
scanf("%d", &len);
scanf("%s", a+);
for(int i = len; i >= ; i--)
b[i] = a[len-i+]; for(int i = ; i <= len; i++)
{
for(int j = ; j <= len; j++){
if(a[i] == b[j]){
dp[i%][j] = dp[(i-)%][j-]+;
}
else{
dp[i%][j] = max(dp[(i-)%][j], dp[i%][j-]);
}
}
}
int ans = len-dp[len%][len];
printf("%d\n", ans); return ;
}