Notice
Recent Posts
Recent Comments
Link
«   2025/02   »
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
Tags
more
Archives
Today
Total
관리 메뉴

코드와이

[BAEKJOON] 10942. 팰린드롬? 본문

acmicpc

[BAEKJOON] 10942. 팰린드롬?

코드와이 2021. 4. 29. 23:48

DP

문제링크

www.acmicpc.net/problem/10942

 

10942번: 팰린드롬?

총 M개의 줄에 걸쳐 홍준이의 질문에 대한 명우의 답을 입력으로 주어진 순서에 따라서 출력한다. 팰린드롬인 경우에는 1, 아닌 경우에는 0을 출력한다.

www.acmicpc.net

 

package acmicpc.Gold2;

import java.util.Scanner;

public class 팰린드롬 {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		StringBuilder sb = new StringBuilder();
		
		int n = sc.nextInt();
		int[] arr = new int[n+1];
		for(int i = 1 ; i <= n ; i++) {
			arr[i] = sc.nextInt();
		}
		
		boolean[][] dp = new boolean[n+1][n+1];
		
		for(int i = 1 ; i <= n ; i++) {
			dp[i][i] = true;
		}
		for(int i = 1 ; i <= n-1 ; i++) {
			if(arr[i+1] == arr[i]) dp[i][i+1] = true;
		}
		
		for(int i = 2 ; i <= n ; i++) {
			for(int j = 1 ; j <= n - i ; j++) {
				if(arr[j] == arr[j+i] && dp[j+1][j+i-1]) {
					dp[j][j+i] = true;
				}
			}
		}
		
		int m = sc.nextInt();
		for(int i = 0 ; i < m ; i++) {
			int s = sc.nextInt();
			int e = sc.nextInt();
			
			if(dp[s][e]) sb.append(1);
			else sb.append(0);
			sb.append("\n");
		}
		sb.setLength(sb.length()-1);
		System.out.println(sb);
		
	}
}

'acmicpc' 카테고리의 다른 글

[BAEKJOON] 1655. 가운데를 말해요  (0) 2021.05.04
[BAEKJOON] 12015. 가장 긴 증가하는 부분 수열 2  (0) 2021.04.29
[BAEKJOON] 1699. 제곱수의 합  (0) 2021.04.27
[BAEKJOON] 9251. LCS  (0) 2021.04.27
[BAEKJOON] 11057. 오르막 수  (0) 2021.04.24