Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <cstring>
- #include <map>
- using namespace std;
- const int maxn = 505;
- int zuma[maxn];
- int dp[maxn][maxn];
- int rec(int i, int j) {
- if(i > j) {
- return 0;
- }
- if(i == j) {
- return 1;
- }
- if(dp[i][j] != -1) {
- return dp[i][j];
- }
- int res = 2e9;
- res = min(res, rec(i + 1, j) + 1);
- if(zuma[i] == zuma[i + 1]) {
- res = min(res, rec(i + 2, j) + 1);
- }
- for(int k = i + 2; k <= j; k++) {
- if(zuma[i] == zuma[k]) {
- res = min(res, rec(i + 1, k - 1) + rec(k + 1, j));
- }
- }
- return dp[i][j] = res;
- }
- int main() {
- ios_base::sync_with_stdio(false);
- int n;
- cin >> n;
- for(int i = 0; i < n; i++) {
- cin >> zuma[i];
- }
- memset(dp, -1, sizeof dp);
- cout << rec(0, n - 1) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement