-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzuma.cpp
More file actions
44 lines (33 loc) · 733 Bytes
/
zuma.cpp
File metadata and controls
44 lines (33 loc) · 733 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 505;
int arr[N];
int dp[N][N];
int dpp (int l, int r) {
if (l == r) {
return 1;
}
if (l > r) {
return 0;
}
if (dp[l][r] != INF) return dp[l][r];
dp[l][r] = min(dp[l][r], dpp(l+1, r)+1);
for (int i = l + 2; i <= r; i++) {
if (arr[l] == arr[i]) {
dp[l][r] = min(dp[l][r], dpp(l+1, i-1)+dpp(i+1, r));
}
}
if (arr[l] == arr[l+1]) {
dp[l][r] = min(dp[l][r], dpp(l+2, r)+1);
}
return dp[l][r];
}
int main ()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> arr[i];
memset(dp, INF, sizeof(dp));
cout << dpp(1, n) << "\n";
}