-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10942.cpp
More file actions
53 lines (44 loc) · 773 Bytes
/
Copy path10942.cpp
File metadata and controls
53 lines (44 loc) · 773 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
45
46
47
48
49
50
51
52
53
#include <iostream>
#include<algorithm>
using namespace std;
int arr[2001];
int dp[2001][2001];
int go(int i, int j) {
if (dp[i][j] >= 0)
return dp[i][j];
if (i >= j)
return dp[i][j] = 1;
if (i + 1 == j) {
if (arr[i] == arr[j])
return dp[i][j] = 1;
else
return dp[i][j] = 0;
}
if (arr[i] == arr[j]) {
dp[i][j] = go(i + 1, j - 1);
}
else {
return dp[i][j] = 0;
}
}
int main(void) {
int N;
scanf("%d", &N);
fill(&dp[0][0], &dp[2001][2001], -1);
for (int i = 1; i <= N; i++) {
scanf("%d", &arr[i]);
}
for (int i = 1; i <= N; i++) {
for (int j = i; j <= N; j++) {
dp[i][j] = go(i, j);
}
}
int M;
scanf("%d", &M);
for (int i = 0; i < M; i++) {
int a, b;
scanf("%d %d", &a, &b);
printf("%d", dp[a][b]);
}
getchar();
}