-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathN10942.cpp
46 lines (41 loc) · 866 Bytes
/
N10942.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
#include <stdio.h>
#include <vector>
using namespace std;
int getAns(int,int);
int T[1000001];
vector<vector<int> > DP;
int main(){
int n;
scanf("%d",&n);
DP.assign(n+1,vector<int>(n+1,-1));
for (int i = 1;i<=n;i++)
scanf("%d",&T[i]);
int testCase;
scanf("%d",&testCase);
int i,j;
int ans;
for(int k = 0;k<testCase;k++){
scanf("%d %d",&i,&j);
ans = getAns(i,j);
printf("%d\n",ans);
}
}
int getAns(int i,int j){
if(i==j)
return 1;
if(j-i == 1 && T[i] == T[j])
return 1;
else if (j-i == 1 && T[i] != T[j])
return 0;
if(DP[i][j] != -1)
return DP[i][j];
if (T[i] != T[j]){
DP[i][j] = 0;
}else{
if(getAns(i+1,j-1) == 1)
DP[i][j] = 1;
else
DP[i][j] = 0;
}
return DP[i][j];
}