Advertisement
Josif_tepe

Untitled

Apr 6th, 2023
443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #include <cstring>
  4. #include <vector>
  5. using ll=long long;
  6.  
  7. using namespace std;
  8.  
  9. int n;
  10. int zuma[555];
  11. int dp[555][555];
  12.  
  13. int rec(int i, int j) {
  14.     if(i == j) {
  15.         return 1;
  16.     }
  17.     if(i > j) {
  18.         return 0;
  19.     }
  20.     if(dp[i][j] != -1) {
  21.         return dp[i][j];
  22.     }
  23.     int result = 2e9;
  24.     result = min(result, rec(i + 1, j) + 1);
  25.     if(zuma[i] == zuma[i + 1]) {
  26.         result = min(result, rec(i + 2, j) + 1);
  27.     }
  28.    
  29.     for(int k = i + 2; k <= j; k++) {
  30.         if(zuma[i] == zuma[k]) {
  31.             result = min(result, rec(i + 1, k - 1) + rec(k + 1, j));
  32.         }
  33.     }
  34.    
  35.     return dp[i][j] = result;
  36. }
  37.  
  38. int main()
  39. {
  40.     cin >> n;
  41.     for(int i = 0; i < n; i++){
  42.         cin >> zuma[i];
  43.     }
  44.     memset(dp, -1, sizeof dp);
  45.     cout << rec(0, n - 1) << endl;
  46.     return 0;
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement