-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path21925_dp.cpp
65 lines (64 loc) · 1.22 KB
/
21925_dp.cpp
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// 210801 #21925 ¦¼ö ÆÓ¸°µå·Ò Gold III
// dp O(N)
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#define all(v) (v).begin(), (v).end()
#define press(v) (v).erase(unique(all(v)), (v).end())
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
const int MAX = 5001;
const int INF = 0x3f3f3f3f;
const ll LNF = 1e18;
const int MOD = 1e9 + 7;
int N, a[MAX], d[MAX], ans;
int flag(int s, int e) {
if (s >= e || !((e - s) % 2))
return 0;
int s1 = s, e1 = e, flag = 1;
while (s1 < e1) {
if (a[s1] != a[e1]) {
flag = 0;
break;
}
s1++; e1--;
}
return flag;
}
int dp(int cur) {
if (cur >= N)
return 0;
int& ret = d[cur];
if (ret != -1)
return ret;
ret = flag(cur, N - 1);
for (int i = cur + 1; i < N - 1; i += 2) {
if (flag(cur, i) && dp(i + 1)) {
ret = max(ret, dp(i + 1) + 1);
}
}
return ret;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> N;
for (int i = 0; i < N; i++) {
cin >> a[i];
}
memset(d, -1, sizeof(d));
if (!dp(0))
cout << -1 << "\n";
else
cout << dp(0) << "\n";
}