๊ธ€ ์ž‘์„ฑ์ž: ์ด์ง€์›๐ŸŒฉ๏ธ

๋ฌธ์ž์—ด์— ์ธ๋ฑ์Šค๋กœ ์ ‘๊ทผํ•˜๊ธฐ ์œ„์— subscript๋ฅผ ์ต์Šคํ…์…˜์œผ๋กœ ๊ตฌํ˜„ํ–ˆ๋‹ค. ํ˜น์€ ๋ฌธ์ž์—ด์„ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•ด์„œ ํ’€์–ด๋„ ๋œ๋‹ค. ๋‚˜๋Š” ๋‘ ๊ฐœ์˜ ์ปค์„œ(๋ฌธ์ž์—ด ์ปค์„œ, ํŒจํ„ด ์ปค์„œ)๋ฅผ ์ด์šฉํ•ด ํ’€์—ˆ๋‹ค.


๋ฌธ์ œ

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


๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ

Swift

extension String {
    subscript(offset: Int) -> String {
        get {
            let index = String.Index(utf16Offset: offset, in: self)
            return String(self[index])
        }
    }
}

let str = readLine()!
let pattern = readLine()!
let n = str.count
let m = pattern.count
var cur = 0 // str cursor
var answer = 0
while cur < n {
    var i = 0 // pattern cursor
    if str[cur] == pattern[i] {
        var isFind = true
        while i < m {
            if cur + i >= n {
                isFind = false
                break
            }
            if str[cur + i] != pattern[i] {
                isFind = false
                break
            }
            i += 1
        }
        if isFind {
            cur += i
            answer += 1
        } else {
            cur += 1
        }
    } else {
        cur += 1
    }
}
print(answer)
๋ฐ˜์‘ํ˜•