백준 10942 팰린드롬?

조항주·2022년 4월 18일
0

algorithm

목록 보기
7/50
post-thumbnail

문제

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

코드

import sys

n = int(input())
lis = list(map(int, input().split()))
m = int(input())
result = ''
dp = [[0]*n for _ in range(n)]
for i in range(n):
    left = i
    right = i
    while left >= 0 and right < n and lis[left] == lis[right]:
        dp[left][right] = 1
        left -= 1
        right += 1

    if i == n-1:
        continue
    left = i
    right = i+1
    while left >= 0 and right < n and lis[left] == lis[right]:
        dp[left][right] = 1
        left -= 1
        right += 1

for i in range(m):
    s, e = map(int, sys.stdin.readline().rstrip().split())
    result += str(dp[s-1][e-1])+'\n'

print(result)

풀이

dp 를 2차원 배열로 만들고 만약 입력 받은 리스트의 s부터 e까지가 팰린드롬이라면 dp[s][e]를 1로 바꿔줬습니다

입력받은 리스트를 순회돌면서 투포인터로 팰린드롬인지 확인해서 dp테이블을 갱신해줬는데 사실 이게 dp로 푼건지는 잘모르겠습니다

0개의 댓글