알고리즘/백준

[백준] 10942번 팰린드롬? (파이썬)

알감자 2022. 1. 4. 20:12
 

10942번: 팰린드롬?

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

www.acmicpc.net

 

코드

import sys

n = int(input())
n_li = list(map(int, sys.stdin.readline().split(" ")))
m = int(input())
m_li = []
for _ in range(m):
    m_li.append(list(map(int, sys.stdin.readline().split(" "))))

check_li = [[False]*n for _ in range(n)]

for i in range(n):
    for j in range(n-i):
        x = j+i
        if j == x:
            check_li[j][x] = True
        elif x - j == 1:
            if n_li[j] == n_li[x]:
                check_li[j][x] = True
            else:
                check_li[j][x] = False
        else:
            if n_li[j] == n_li[x]:
                if check_li[j+1][x-1] == True:
                    check_li[j][x] = True

for i in range(m):
    x, y = m_li[i]
    if check_li[x-1][y-1] == True:
        print(1)
    else:
        print(0)