大意: 给定字符串, 每次删除一个回文子串, 求最少多少次删完.
#include <iostream> #include <cstdio> #define REP(i,a,n) for(int i=a;i<=n;++i) using namespace std; const int N = 510, INF = 0x3f3f3f3f; int n,a[N],dp[N][N]; int main() { scanf("%d",&n); REP(i,1,n) scanf("%d",a+i); REP(d,1,n) { for (int l=1,r=d;r<=n;++l,++r) { if (d==1) dp[l][r]=1; else if (d==2) dp[l][r]=a[l]==a[r]?1:2; else { dp[l][r]=INF; if (a[l]==a[r]) dp[l][r]=dp[l+1][r-1]; REP(k,l,r-1) dp[l][r]=min(dp[l][r],dp[l][k]+dp[k+1][r]); } } } printf("%d ", dp[1][n]); }