์๊ฐ ์ ํ | ๋ฉ๋ชจ๋ฆฌ ์ ํ | ์ ๋ต ๋น์จ |
0.5 ์ด | 256 MB | 28.087% |
๋ฌธ์
ํด๊ฒฐ
key point, ๊ธธ์ด๊ฐ 1, 2, ... N ๊น์ง ํฐ๋ฆฐ๋๋กฌ์ธ์ง ํ์ธํ๋ค. DP
- ๊ธธ์ด๊ฐ 1์ผ ๋๋ ๋ชจ๋ ํฐ๋ฆฐ๋๋กฌ
- ๊ธธ์ด๊ฐ 2์ผ ๋๋ i, j ์ ๋์ด ๊ฐ์์ผ ํฐ๋ฆฐ๋๋กฌ
- ๊ธธ์ด๊ฐ 3์ด์์ผ ๋๋ i, j ์ ๋์ด ๊ฐ์ผ๋ฉด์ dp[i+1][j-1]์ด true์ด๋ฉด ํฐ๋ฆฐ๋๋กฌ์ด๋ค.
์ฝ๋(์ฌ๊ท)
๋ฉ๋ชจ๋ฆฌ | ์๊ฐ |
17664 KB | 300 ms |
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
|
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
int arr[2001];
int dp[2001][2001];
int n, m;
int go(int i, int j) {
int& ret = dp[i][j];
if (i == j) {
ret = 1;
return ret;
}
if (i + 1 == j) {
if (arr[i] == arr[j]) {
ret = 1; return ret;
}
else {
ret = 0; return 0;
}
}
if (ret > 0)return ret;
if (arr[i] != arr[j]) {
ret = 0; return ret;
}
else return ret = go(i + 1, j - 1);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &arr[i]);
memset(dp, -1, sizeof(dp));
scanf("%d", &m);
while (m--) {
int s, e;
scanf("%d %d", &s, &e);
printf("%d\n", go(s, e));
}
return 0;
}
|
cs |
์ฝ๋(๋ฐ๋ณต๋ฌธ)
๋ฉ๋ชจ๋ฆฌ | ์๊ฐ |
5028 KB | 300 ms |
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
|
#include <cstdio>
int n;
int a[2000];
bool d[2000][2000];
int main() {
scanf("%d",&n);
for (int i=0; i<n; i++) {
scanf("%d",&a[i]);
}
for (int i=0; i<n; i++) {
d[i][i] = true;
}
for (int i=0; i<n-1; i++) {
if (a[i] == a[i+1]) {
d[i][i+1] = true;
}
}
for (int k=3; k<=n; k++) {
for (int i=0; i<=n-k; i++) {
int j = i+k-1;
if (a[i] == a[j] && d[i+1][j-1]) {
d[i][j] = true;
}
}
}
int m;
scanf("%d",&m);
while (m--) {
int s, e;
scanf("%d %d",&s,&e);
printf("%d\n",d[s-1][e-1]);
}
return 0;
}
|
cs |
'๐ฅ PS(Problem Solving) ๐ฅ > BOJ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ] #10868 ์ต์๊ฐ (0) | 2021.02.15 |
---|---|
[BOJ] #1509 ํฐ๋ฆฐ๋๋กฌ ๋ถํ (0) | 2021.02.09 |
[BOJ] #2632 ํผ์ํ๋งค (0) | 2021.02.05 |
[BOJ] #1208 ๋ถ๋ถ์์ด์ ํฉ 2 (0) | 2021.02.04 |
[BOJ] #12100 2048 (0) | 2020.09.18 |
๋๊ธ