好吧,在几天一直做水题,找找手感,过两天开刷难题。
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN = 5010;
short int dp[2][MAXN];
int main(){
int n;
string s1, s2;
while(~scanf("%d", &n)){
cin >> s1;
s2.reserve(n);
for(int i = n-1;i >= 0;i --) s2[n-i-1] = s1[i];
memset(dp, 0, sizeof dp);
for(int i = 1;i <= n;i ++){
for(int j = 1;j <= n;j ++){
if(s1[i-1] == s2[j-1]) dp[i%2][j] = dp[(i-1)%2][j-1] + 1;
else dp[i%2][j] = max(dp[i%2][j-1], dp[(i-1)%2][j]);
}
}
printf("%d
", n-dp[n%2][n]);
}
return 0;
}