ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป ๋ฌธ์ œ

 

1802๋ฒˆ: ์ข…์ด ์ ‘๊ธฐ

์ฒซ์งธ ์ค„์— ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ๊ฐœ์ˆ˜ T๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. T๋Š” 1000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ T๊ฐœ์˜ ์ค„์— ๊ฐ๊ฐ์˜ ์ข…์ด๊ฐ€ ์–ด๋–ป๊ฒŒ ์ ‘ํ˜€์žˆ๋Š”์ง€๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ข…์ด์˜ ์ •๋ณด๋Š” ๋ฌธ์ž์—ด๋กœ ์ฃผ์–ด์ง€๋ฉฐ, 1

www.acmicpc.net


โœ๐Ÿป ํ’€์ด

๐ŸŽจ Go

// https://www.acmicpc.net/problem/1802
package main

import (
	"bufio"
	"fmt"
	"os"
	"strings"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var t int
	fmt.Fscanln(reader, &t)

	for i := 0; i < t; i++ {
		input, _ := reader.ReadString('\n')
		rules := strings.Split(strings.Replace(input, "\n", "", 1), "")
		if check(rules) {
			fmt.Fprintln(writer, "YES")
		} else {
			fmt.Fprintln(writer, "NO")
		}
	}
}

func check(rules []string) bool {
	if len(rules) == 1 {
		return true
	}
	for i := 0; i < len(rules)/2; i++ {
		if rules[i] == rules[len(rules)-1-i] { // ๊ฐ€์šด๋ฐ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ ‘ํžˆ๋Š” ๋ถ€๋ถ„์€ ์„œ๋กœ ๋‹ค๋ฅธ ๊ฐ’์ด์–ด์•ผ ํ•จ
			return false
		}
	}
	return check(rules[0:len(rules)/2]) && check(rules[len(rules)/2+1:len(rules)]) // ๊ฐ€์šด๋ฐ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์™ผ์ชฝ๊ณผ ์˜ค๋ฅธ์ชฝ์„ ์ฒดํฌ
}

๐ŸŽจ Python3

# https://www.acmicpc.net/problem/1802
import sys

def check(rules):
    if len(rules) == 1:
        return True
    for i in range(len(rules)//2):
        if rules[i] == rules[len(rules)-1-i]:
            return False
    return check(rules[0:len(rules)//2]) and check(rules[len(rules)//2+1:len(rules)])

if __name__ == "__main__":
    t = int(sys.stdin.readline())
    for i in range(t):
        rules = list(sys.stdin.readline().rstrip())
        if check(rules):
            print("YES")
        else:
            print("NO")
728x90
๋Œ“๊ธ€