I - Palindrome(LCS&字符串)

AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
const int N=5e3+5;
int dp[2][N];//滚动数组减小空间. 
int main(){ 
	int n;
	while(~scanf("%d",&n)){
		memset(dp,0,sizeof dp);
		string a,b;
		cin>>a;
		b=a;
		int l=a.size();
		reverse(b.begin(),b.end());
		for(int i=1;i<=l;i++) //LCS板子 
		{
			for(int j=1;j<=l;j++)
			{
				 if(a[i-1]==b[j-1])
				 {
				 	  dp[i%2][j]=dp[(i-1)%2][j-1]+1;
				 }
				 else dp[i%2][j]=max(dp[(i-1)%2][j],dp[i%2][j-1]);
			}
		}
		printf("%d\n",l-dp[l%2][l]);
	}
	return 0;
}