๋ฐฑ์ค€ - ํšŒ๋ฌธ(Swift)

2022. 10. 6. 11:03ใ†Algorithm

๋ฐฑ์ค€ - ํšŒ๋ฌธ(Swift)

 

 

 ๋ฌธ์ œ ์„ค๋ช…

 

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

 

17609๋ฒˆ: ํšŒ๋ฌธ

๊ฐ ๋ฌธ์ž์—ด์ด ํšŒ๋ฌธ์ธ์ง€, ์œ ์‚ฌ ํšŒ๋ฌธ์ธ์ง€, ๋‘˜ ๋ชจ๋‘ ํ•ด๋‹น๋˜์ง€ ์•Š๋Š”์ง€๋ฅผ ํŒ๋‹จํ•˜์—ฌ ํšŒ๋ฌธ์ด๋ฉด 0, ์œ ์‚ฌ ํšŒ๋ฌธ์ด๋ฉด 1, ๋‘˜ ๋ชจ๋‘ ์•„๋‹ˆ๋ฉด 2๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์ถœ๋ ฅํ•œ๋‹ค.

www.acmicpc.net

 

 

 

 ๋‚˜์˜ ํ’€์ด

 

์ด ํ’€์ด๋กœ ์˜ˆ์ œ์˜ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋ฅผ ๊ฒ€์‚ฌํ•˜๋ฉด ์ „๋ถ€ ๋งž๋‹ค.

ํ•˜์ง€๋งŒ ์‹ค์ œ๋กœ ์ œ์ถœํ•˜๋ฉด ์ฒ˜์Œ๋ถ€ํ„ฐ ์‹คํŒจํ•˜๋Š” ๊ฑธ ๋ณผ ์ˆ˜ ์žˆ๋‹ค..

 

์ด์œ ๋Š” ์—ญ์‹œ ๋ฐ˜๋ก€๋ฅผ ์ƒ๊ฐํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

์ด ๋ฌธ์ œ๋Š” ์–‘์ชฝ์ด ๊ฐ™์ง€์•Š์„๋•Œ ์™ผ์ชฝ์„ ์ถ”์ถœํ•ด์„œ ์ง„ํ–‰ํ•˜๋Š” ๊ฒฝ์šฐ์™€ ์˜ค๋ฅธ์ชฝ์„ ์ถ”์ถœํ•ด์„œ ์ง„ํ–‰ํ•˜๋Š” ๊ฒฝ์šฐ ๋‘ ๊ฐ€์ง€๋ฅผ ๊ฐ๊ฐ ํƒ์ƒ‰ํ•ด์ค˜์•ผํ•˜๋Š”๋ฐ,

์•„๋ž˜ ๋‚ด ์ฝ”๋“œ๋Š” ์™ผ์ชฝ์ด ์ถ”์ถœ๋œ๋‹ค๋ฉด ์™ผ์ชฝ๋งŒ ๊ฐ€๊ณ  ์˜ค๋ฅธ์ชฝ์˜ ๊ฒฝ์šฐ๋Š” ํƒ์ƒ‰ํ•˜์ง€ ์•Š๊ฒŒ ๋œ๋‹ค.(์˜ค๋ฅธ์ชฝ์„ ๋จผ์ € ์ถ”์ถœํ–ˆ์–ด์•ผ ์œ ์‚ฌํšŒ๋ฌธ์ด์—ˆ์„ ์ˆ˜๋„ ์žˆ๋Š”๋ฐ ๋ง์ด๋‹ค)

 

๊ทธ๋ž˜์„œ ๋‹ค๋ฅธ์‚ฌ๋žŒ์ด ์žฌ๊ท€๋ฅผ ์ด์šฉํ•œ ๊ฒƒ์„ ๋ณด๊ณ  ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•ด์˜€๋‹ค.

 

import Foundation
let n = Int(readLine()!)!
var results: [Int] = []
func checkRotate(_ array: [Character]) -> Int {
    var start = 0
    var end = array.count-1
    var cnt = 0
    while start < end {
        if cnt > 1 {
            return 2
        }
        if array[start] == array[end] {
            start += 1
            end -= 1
        } else if array[start+1] == array[end] {
            start += 1
            cnt += 1
        } else if array[start] == array[end-1] {
            end -= 1
            cnt += 1
        } else {
            return 2
        }
    }
    if cnt == 1 {
        return 1
    } else {
        return 0
    }
}

for _ in 0..<n {
    let input = Array(readLine()!.lowercased())
    results.append(checkRotate(input))
}

for x in results {
    print(x)
}

 

 

 ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด

 

์ด ํ’€์ด์˜ ํ•ต์‹ฌ์€ ๊ฐ๊ฐ์˜ ์ผ€์ด์Šค๋ฅผ if๋ฌธ์„ ํ†ตํ•ด ์žฌ๊ท€๋กœ ๋Œ์•„๊ฐ€๋ฉด์„œ ๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ํƒ์ƒ‰ํ•˜๋Š” ๊ฒƒ์ด๋‹ค

 

๊ทธ๋Ÿฐ๋ฐ "์ฒซ๋ฒˆ์งธ if๋ฌธ์—์„œ(๋จผ์ € ์™ผ์ชฝ์„ ์ถ”์ถœํ–ˆ์„๋•Œ) ์œ ์‚ฌํšŒ๋ฌธ์„ result์— ๋‹ด์•˜๋Š”๋ฐ ๋‘๋ฒˆ์งธ if๋ฌธ์—์„œ ์œ ์‚ฌํšŒ๋ฌธ์ด ์•„๋‹Œ๊ฒŒ ๋‹ด๊ฒจ์งˆ ์ˆ˜ ์žˆ๋Š” ๊ฑฐ ์•„๋‹Œ๊ฐ€?" ๋ผ๋Š” ์ƒ๊ฐ์ด ๋“ค ์ˆ˜ ์žˆ๋‹ค.

ํ•˜์ง€๋งŒ ์ด๋Š” ๋ฌธ์ œ๊ฐ€ ํ•œ์ •์ ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ƒ๊ฐ์„ ์ข€ ํ•ด๋ณด๋ฉด ์‰ฝ๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ผ๋‹จ ํ•œ๋ฒˆ ๊ฐ’์„ ์ถ”์ถœํ•œ ์ƒํƒœ(์™ผ์ชฝ์ด๋“  ์˜ค๋ฅธ์ชฝ์ด๋“ )๋Š” ์œ ์‚ฌํšŒ๋ฌธ์ธ ์ƒํƒœ์ด๋‹ค. ๊ทธ ์ƒํƒœ์—์„œ ์ญ‰ ๋ฌธ์ œ์—†์ด ๊ฐ ๋ฌธ์ž๊ฐ€ ๊ฐ™๋‹ค๋ฉด ์œ ์‚ฌํšŒ๋ฌธ์ธ๋ฐ, ๋ฌธ์ œ๊ฐ€ ์žˆ์„๋•Œ๋Š” if cnt == 1 ์—์„œ ๊ฑธ๋ฆฌ๊ณ  return์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ฒซ๋ฒˆ์งธ if๋ฌธ์—์„œ์˜ ์œ ์‚ฌํšŒ๋ฌธ์„ etc๋กœ ๋ฐ”๋€Œ๋Š” ๊ฒฝ์šฐ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค.

 

enum PalinType: Int {
    case palin = 0
    case psuedo = 1
    case etc = 2
}

func isPalin(_ arr: [Character], _ s: Int, _ e: Int, _ cnt: Int) {
    if (s < e) {
        if arr[s] == arr[e] {
            isPalin(arr, s+1, e-1, cnt)
        } else {
            // ํ•˜๋‚˜๋ฅผ ์ถ”์ถœํ•œ ์ƒํƒœ์—์„œ ๋˜ ์–‘์ชฝ์ด ๊ฐ™์ง€์•Š์€ ๊ฒฝ์šฐ๋Š” ํšŒ๋ฌธ๋„ ์œ ์‚ฌํšŒ๋ฌธ๋„ ์•„๋‹Œ ์ƒํƒœ์ด๋‹ค.
            if cnt == 1 {
                return
            }
            if arr[s+1] == arr[e] {
                isPalin(arr, s+1, e, cnt+1)
            }
            
            if arr[s] == arr[e-1] {
                isPalin(arr, s, e-1, cnt+1)   
            }
            
            if arr[s+1] != arr[e] && arr[s] != arr[e-1] {
                return
            } 
        }
    } else {
        // ์‹ค์ œ๋กœ cnt๋Š” 0, 1์ด์™ธ์˜ ๊ฐ’์ด ์˜ฌ ์ˆ˜ ์—†๋‹ค. ์™œ๋ƒํ•˜๋ฉด cnt๊ฐ€ 1์ผ๋–„ ์ข…๋ฃŒ์‹œ์ผœ๋ฒ„๋ฆฌ๊ธฐ ๋–„๋ฌธ์ด๋‹ค.
        switch cnt {
            case 0:
            result = .palin
            case 1:
            result = .psuedo
            default:
            result = .etc
        }
    }
}

let n = Int(readLine()!)!
var result: PalinType = .etc
for _ in 0..<n {
    let input = Array(readLine()!)
    isPalin(input, 0, input.count-1, 0)
    print(result.rawValue)
    result = .etc
}

 

 ํ”ผ๋“œ๋ฐฑ

 

1. enumํƒ€์ž…์„ ์›์‹œ๊ฐ’์„ ์ด์šฉํ•ด์„œ ๋ณ€์ˆ˜์™€ ์ˆซ์ž ์‚ฌ์ด๋ฅผ ์œ ๋™์ ์œผ๋กœ ์‚ฌ์šฉํ•˜์ž.

2. ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ํƒ์ƒ‰ํ•  ๋•Œ๋Š” ์žฌ๊ท€๊ฐ€ ์ข‹๋‹ค.