๋ฐฑ์ค€ - ๋ฌธ์„œ ๊ฒ€์ƒ‰(Swift)

2022. 10. 26. 16:41ใ†Algorithm

๋ฌธ์ œ ์ด๋ฆ„

 

 

 ๋ฌธ์ œ ์„ค๋ช…

 

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

 

1543๋ฒˆ: ๋ฌธ์„œ ๊ฒ€์ƒ‰

์„ธ์ค€์ด๋Š” ์˜์–ด๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ์–ด๋–ค ๋ฌธ์„œ๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค๋ ค๊ณ  ํ•œ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ์–ด๋–ค ๋‹จ์–ด๊ฐ€ ์ด ๋ช‡ ๋ฒˆ ๋“ฑ์žฅํ•˜๋Š”์ง€ ์„ธ๋ ค๊ณ  ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜, ์„ธ์ค€์ด์˜ ํ•จ์ˆ˜๋Š” ์ค‘๋ณต๋˜์–ด ์„ธ๋Š” ๊ฒƒ์€ ๋นผ๊ณ  ์„ธ์•ผ ํ•œ

www.acmicpc.net

 

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

 

์ฒ˜์Œ์—๋Š” ๋‹จ์ˆœํžˆ ์ฒซ ๋ฒˆ์งธ๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•˜์—ฌ target๊ณผ ๋งž์ง€์•Š์œผ๋ฉด idx๋ฅผ ๋ฆฌ์…‹ํ•˜๋Š” ์‹์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ค๊ฐ€ ๋„˜๊ธด๊ฐ’์ด target[idx]๊ฐ’๊ณผ ๊ฐ™๋‹ค๋ฉด ์ด์–ด์„œ ์ธ๋ฑ์Šคํƒ์ƒ‰์„ ํ•ด์ค˜์•ผํ•˜๋Š” ๋ฐ˜๋ก€๋ฅผ ์ฐพ์•˜๋‹ค.

ํ•˜์ง€๋งŒ ๊ทธ๋ž˜๋„ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋ฅผ ์ „๋ถ€ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ–ˆ๋‹ค.

 

๊ทธ๋ž˜์„œ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด๋ฅผ ์ฐพ์•„๋ดค๋‹ค.

 

import Foundation

let document = readLine()!.map{String($0)}
let target = readLine()!.map{String($0)}
var cnt = 0
var idx = 0
for i in 0...document.count-1 {
    if target[idx] == document[i] {
        idx += 1
        if idx == target.count {
            cnt += 1
            idx = 0
        }
    } else { 
        idx = 0
        if target[idx] == document[i] {
            idx += 1
        }
    }
}

print(cnt)

 

 

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

 

import Foundation

var doc = readLine()!
let target = readLine()!
var cnt = 0
var index = doc.range(of: target)?.upperBound

while index != nil {
    cnt += 1
    doc = String(doc[index!...])
    index = doc.range(of: target)?.upperBound
}

print(cnt)

 

 

์Šค์œ„ํ”„ํŠธ์—์„œ ๋ฌธ์ž์—ด์„ ์–ด๋–ป๊ฒŒ ๋‹ค๋ฃจ๊ณ  ์žˆ๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ๋Š” ํ’€์ด์ด๋‹ค.

๋จผ์ € ๋ฌธ์ž์—ด์— ๋Œ€ํ•ด range(of:)๋ฅผ ํ•˜๋ฉด ๋ฐฐ์—ด๋กœ ๋”ฐ์กŒ์„๋•Œ 0..<์ฐพ๋Š” ๋ฌธ์ž์—ด์˜ ๋+1์ด ๋œ๋‹ค. ์ฆ‰ ์—ฌ๊ธฐ์„œ upperBound๋ฅผ ํ•˜๊ฒŒ๋˜๋ฉด ์ฐพ๋Š” ๋ฌธ์ž์—ด์˜ ๋ +1์ธ ์ธ๋ฑ์Šค๊ฐ€ ๋‚˜์˜ค๊ฒŒ ๋˜๊ณ  doc[index!...]๋ฅผ ํ•˜๊ฒŒ๋˜๋ฉด ๊ทธ ๋ถ€๋ถ„๋ถ€ํ„ฐ ๋‹ค์‹œ ๋ฌธ์ž์—ด์„ ๊ตฌ์„ฑํ•˜๊ฒŒ ๋œ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  ์ƒˆ๋กœ ์•Œ๊ฒŒ๋œ ์‚ฌ์‹ค์€ 

var str: String = "abc"์ผ๋•Œ str[str.endIndex...]๋Š” ๋Ÿฐํƒ€์ž„์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค์ง€ ์•Š๋Š” ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์ด ๋•Œ๋ฌธ์— ์œ„์˜ while๋ฌธ์—์„œ doc[index!...]๋ฅผ ํ• ๋•Œ index๊ฐ€ boundary์™ธ์˜ ๊ฐ’์ด๋ผ๋„ ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค์ง€ ์•Š๋Š”๊ฒƒ์ด๋‹ค.

 

 

 

 

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

 

๋‚˜์˜ ๋ฐฉ๋ฒ•์—์„œ ๋ฐ˜๋ก€๋ฅผ ๋ชป์ฐพ์„๋•Œ ๊ทธ๋ƒฅ ์ „๋ถ€๋ฅผ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

๊ทธ ๋ฐฉ๋ฒ•์ด ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์ด๋‹ค. 

ํ•ญ์ƒ ์™„์ „ํƒ์ƒ‰๋„ ๋ฐฉ๋ฒ•์ด๋ผ๋Š” ๊ฒƒ์„ ์ธ์ง€ํ•˜๊ณ  ๋ฌธ์ œ๋ฅผ ํ’€์–ด์•ผ๊ฒ ๋‹ค.

 

import Foundation

var doc = Array(readLine()!)
let target = Array(readLine()!)

func findMatching(_ idx: Int) -> Int {
    var i = idx
    var tIdx = 0
    var cnt = 0
    while i < doc.count {
        if target[tIdx] == doc[i] {
            if tIdx == target.count-1 {
                cnt += 1
                tIdx = 0
            } else {
                tIdx += 1
            }
        } else {
            tIdx = 0
        }
        i += 1
    }
    return cnt
}

var maxVal = 0
for i in 0..<doc.count {
    maxVal = max(maxVal, findMatching(i))
}

print(maxVal)