题目链接:https://www.luogu.com.cn/problem/CF607B
解题思路(区间DP)完全参照自 QwQcOrZ大佬的博客:https://www.luogu.com.cn/blog/61120/solution-cf607b
示例代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 505;
int f[maxn][maxn], a[maxn], n;
bool vis[maxn][maxn];
int dfs(int L, int R) {
if (L == R) return 1;
if (L+1 == R) return 1 + (a[L] != a[R]);
if (vis[L][R]) return f[L][R];
vis[L][R] = true;
f[L][R] = (1<<29);
if (a[L] == a[R]) f[L][R] = min(f[L][R], dfs(L+1, R-1));
for (int i = L; i < R; i ++)
f[L][R] = min(f[L][R], dfs(L, i) + dfs(i+1, R));
return f[L][R];
}
int main() {
cin >> n;
for (int i = 1; i <= n; i ++) cin >> a[i];
cout << dfs(1, n) << endl;
return 0;
}