Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <map>
- #include <cstring>
- #include <vector>
- using ll=long long;
- using namespace std;
- int n;
- int zuma[555];
- int dp[555][555];
- int rec(int i, int j) {
- if(i == j) {
- return 1;
- }
- if(i > j) {
- return 0;
- }
- if(dp[i][j] != -1) {
- return dp[i][j];
- }
- int result = 2e9;
- result = min(result, rec(i + 1, j) + 1);
- if(zuma[i] == zuma[i + 1]) {
- result = min(result, rec(i + 2, j) + 1);
- }
- for(int k = i + 2; k <= j; k++) {
- if(zuma[i] == zuma[k]) {
- result = min(result, rec(i + 1, k - 1) + rec(k + 1, j));
- }
- }
- return dp[i][j] = result;
- }
- int main()
- {
- 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