๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿƒ algorithm/boj

BOJ 1802 - ์ข…์ด ์ ‘๊ธฐ

by HandHand 2021. 3. 8.

๋ฌธ์ œ

๋ฐฑ์ค€ ์˜จ๋ผ์ธ ์ €์ง€ - 1802๋ฒˆ

ํ’€์ด ๊ณผ์ •

๊ทœ์น™์— ๋”ฐ๋ผ ์ข…์ด๋ฅผ ์ ‘์—ˆ๋‹ค๊ฐ€ ํˆ์„ ๋•Œ ๋ชฉํ‘œ๋กœ ํ•˜๋Š” ์ƒํƒœ๊ฐ€ ๋  ์ˆ˜ ์žˆ๋Š”์ง€ ํŒ๋‹จํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

์ข…์ด ์ ‘๊ธฐ์— ์˜ํ•ด ์ƒ๊ธฐ๋Š” ๊บพ์ž„ ๋ฐฉํ–ฅ์—๋Š” ๊ทœ์น™์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.
์šฐ์„  ์ค‘์‹ฌ์„ ๊ธฐ์ค€์œผ๋กœ ๋ฐ˜์œผ๋กœ ์ ‘๊ธฐ ๋•Œ๋ฌธ์— ์ค‘์‹ฌ ์œ„์น˜์˜ ๊บพ์ž„ ์œ„์น˜๋Š” ์ค‘์š”ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
๋‹ค๋งŒ ์ ‘ํžˆ๋Š” ์ค‘์‹ฌ์„ ๊ธฐ์ค€์œผ๋กœ ์ขŒ, ์šฐ๊ฐ€ ์„œ๋กœ ๋Œ€์นญ์„ ์ด๋ฃจ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
๋”ฐ๋ผ์„œ ๋ถ„ํ•  ์ •๋ณต ์„ ํ†ตํ•ด ๋Œ€์นญ์„ฑ์„ ํŒ๋‹จํ•ด์„œ ๋‹ต์„ ๊ตฌํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์ฝ”๋“œ


import sys


def check(paper, mid):
    for i in range(mid):
        if paper[i] == paper[mid * 2 - i]:
            return False

    return True


def check_opposite(paper):
    if len(paper) == 1:
        return True

    mid = len(paper) // 2
    ret = check(paper, mid) and check_opposite(paper[:mid]) and check_opposite(paper[mid + 1:])

    return ret


def solution(paper):
    is_opposite = check_opposite(paper)
    return 'YES' if is_opposite else 'NO'


if __name__ == '__main__':
    T = int(input())
    for _ in range(T):
        paper = sys.stdin.readline().strip()
        answer = solution(paper)
        print(answer)
๋ฐ˜์‘ํ˜•

'๐Ÿƒ algorithm > boj' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

BOJ 14226 - ์ด๋ชจํ‹ฐ์ฝ˜  (0) 2021.03.08
BOJ 2529 - ๋ถ€๋“ฑํ˜ธ  (0) 2021.03.08
BOJ 17829 - 222-ํ’€๋ง  (0) 2021.03.08
BOJ 13905 - ์„ธ๋ถ€  (0) 2021.03.08
BOJ 2458 - ํ‚ค ์ˆœ์„œ  (0) 2021.03.08

๐Ÿ’ฌ ๋Œ“๊ธ€