[BOJ] 10942 팰린드롬?

Time Lapse : 50sec

10942.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
#include <iostream>
#include <memory.h>

using namespace std;

#define endl '\n'

int N,M,S,E;
int dp[2001][2001];
int arr[2001];

int func(int s, int e)
{
if(s==e)
return 1;
if(s+1==e&&arr[s]==arr[e])
return 1;

int &ret = dp[s][e];
if(ret!=-1)
return ret;
ret = 0;
if(arr[s]==arr[e])
ret = func(s+1,e-1);

return ret;
}

int main(void)
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>N;
for(int i=1;i<=N;i++)
cin>>arr[i];

memset(dp,-1,sizeof(dp));
cin>>M;
for(int i=0;i<M;i++)
{
cin>>S>>E;
cout<<func(S,E)<<endl;
}

}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/23/PS/BOJ/10942/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.