1 minute read

๐Ÿ“Œ ๋‚œ์ด๋„

๐Ÿฅ‡ Gold 4


๐Ÿ“Œ ๋ฌธ์ œ

https://www.acmicpc.net/problem/10942


image

image


๐Ÿ“Œ ํ’€์ด


๐Ÿ“Œ Code

package BOJ.DP;


import java.io.*;
import java.util.StringTokenizer;

import static BOJ.DP.N2293_F.MakeDP;

public class N10942 {
    static int N;
    static int M;
    static int[] nums;
    static boolean[][] dp;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        N = Integer.parseInt(br.readLine());
        nums = new int[N];
        dp = new boolean[N][N];
        st = new StringTokenizer(br.readLine());
        for (int i = 0; i < N; i++) {
            nums[i] = Integer.parseInt(st.nextToken());
        }
        MakeDP();
        M = Integer.parseInt(br.readLine());
        for (int i = 0; i < M; i++) {
            st = new StringTokenizer(br.readLine());
            if (dp[Integer.parseInt(st.nextToken()) - 1][Integer.parseInt(st.nextToken()) - 1]) {
                bw.write(1 + "\n");
            } else {
                bw.write(0 + "\n");
            }
        }
        bw.flush();
        bw.close();
    }

    static void MakeDP() {
        for (int i = 0; i < N; i++) {
            dp[i][i] = true;
            int range = i;
            range = Math.min(range, N - i - 1);
            for (int j = 1; j <= range; j++) {
                if (dp[i - j + 1][i + j - 1] && nums[i - j] == nums[i + j]) {
                    dp[i - j][i + j] = true;
                }
            }
        }
        for (int i = 1; i < N; i++) {
            if (nums[i - 1] == nums[i]) {
                dp[i - 1][i] = true;
            }
        }
        for (int i = 1; i < N - 1; i++) {
            int range = i;
            range = Math.min(range, N - i - 2);
            for (int j = 1; j <= range; j++) {
                if (dp[i - j + 1][i + j] && nums[i - j] == nums[i + j]) {
                    dp[i - j][i + j + 1] = true;
                }
            }
        }
    }
}

package (์ด๋ฆ„); ๋ฅผ ๋•Œ๊ณ , class ์ด๋ฆ„์„ Main์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ๋œ๋‹ค.


๐Ÿ“Œ ์ถ”๊ฐ€ ์˜ˆ์ œ

[Input]
12
1 2 1 3 1 2 1 1 1 1 1 1
10
1 3
2 5
3 3
5 7
5 5
1 7
8 12
8 11
8 10
8 9

[Output]
1
0
1
1
1
1
1
1
1
1
[Input]
12
1 1 1 1 1 1 1 1 1 1 1 1
10
1 2
2 3
1 5
4 8
2 5
6 8
1 10
2 12
2 2
9 12

[Output]
1
1
1
1
1
1
1
1
1
1



๊ฐœ์ธ ๊ณต๋ถ€ ๊ธฐ๋ก์šฉ ๋ธ”๋กœ๊ทธ์ž…๋‹ˆ๋‹ค.
ํ‹€๋ฆฌ๊ฑฐ๋‚˜ ์˜ค๋ฅ˜๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ ์ œ๋ณดํ•ด์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.๐Ÿ˜