-
Notifications
You must be signed in to change notification settings - Fork 0
3-giljihun #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
3-giljihun #15
Conversation
giljihun/๋ฌธ์์ด/[BOJ] IOIOI.swift
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ ๋ ฅ๋ฐ๋ ๋ถ๋ถ ์ ํ์ฉํ์ต๋๋ค.. ๐
giljihun/๋ฌธ์์ด/[BOJ] IOIOI.swift
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ด ๋ถ๋ถ๋ง ์ด์ง๋ค๋ฅด๋ค์! ์ ๋ Array์ ๋ด์์คฌ๋๋ฐ, ์ด๋ ๊ฒํ๋ฉด ๋์ค์ filter,map,reduce์ฐ๋ฉด์ ์ฒ๋ฆฌํด์ฃผ๋๋ผ ์๊ฐ,๋ฉ๋ชจ๋ฆฌ ๋ค ์ข ์ํด๋ณด๋ ๋๋์ด์๊ฑฐ๋ ์..
๊ธธ์ ์๋์ด ํธ์ ๊ฒ ๋ ์ง๊ด์ ์ด๊ณ ๋ฉ๋ชจ๋ฆฌ์๋ ํจ์จ์ ์ธ ๊ฒ ๊ฐ์ต๋๋ค..!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๊ฐ์ฌํฉ๋๋ค ใ
ใ
์ Array์ ๋ด์์ ์ฒ๋ฆฌํ๋ ๋ฐฉ์์ด ๊ฐ๋
์ฑ์ ๋ ์ข์ ์๋ ์์ ๊ฒ ๊ฐ์์
์ง๊ธ ๊ตฌ์กฐ๊ฐ ํจํด์ ์์ง -> ํ์ฒ๋ฆฌํ๋ ๊ตฌ์กฐ์ธ๋ฐ,
์ค๊ฐ ๋ก์ง์ด ๋ณต์กํ๋ค๋ฉด ์ฌ์ค ์ ๊ฐ ์์ฑํ ์ฝ๋๋ก๋ ํ๋ฆ์ ๋ถ๋ฆฌํ๊ณ ํ
์คํ
ํ๊ธฐ์ ์ด๋ ค์ธ ์๋ ์์ ๊ฒ ๊ฐ์ต๋๋ค. ๋ฉ๋ชจ๋ฆฌ ์ํด์ ๋ํ ๋ด์ฉ์ ์ ํํ ์บ์น๋ฅผ ํ์ ๊ฒ ๊ฐ๋ค์!! ๊ตฟ์
๋๋ค
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์๋ ์ฝ๋๋ ํ์ธํด๋ณด๋ ํ์ฒ๋ฆฌ๋ฅผ ํด๋ก์ ๋ก ํ ๋ฒ์ ์กฐ์ง์๋(?) ๋ถ๋ถ์ด ์ผ๋ฌด์ง๋ค์..
bishoe01
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import Foundation
func IOIOI(_ time: Int, _ length: Int, _ s: String) {
let arr = Array(s)
var cnt = 0
var cntArr = [Int]()
var cursor = 0
while cursor < length - 2 {
// print(cursor)
if arr[cursor] == "I", arr[cursor + 1] == "O", arr[cursor + 2] == "I" {
cnt += 1
cursor += 2
} else {
cntArr.append(cnt)
cnt = 0
cursor += 1
}
}
cntArr.append(cnt) // ๋จ์์
// print(cntArr.filter { item in
// item >= time
// }.reduce(0, +)) ์ด ๋ถ๋ถ ํ๋ ค์ ์์ ํ์, ์ฐ์๋๊ฑฐ์ ๋งค๋ชฐ๋ผ์, ๊ทธ๊ฒ ์๋๋ผ ์ฐ์๋๊ฑฐ์์ - ๋ฐ๋ณต์ + 1 ํด์ค์ผ ์ง์ง ์ํ๋ ๊ฐ์๊ฐ ๋์ด ๋ฑ๋ง์ถฐ์ ๋ ์๋ ์์ผ๋๊น
print(cntArr.filter { $0 >= time }.map { $0 - time + 1 }.reduce(0, +))
}์ฒ์์๋ String.Index์จ์ ๊ณ์ slicingํด์ฃผ๋ฉด์ ์ฐพ์์ฃผ๋ คํ๋๋ฐ, (์ฝ๊ฐ ์ฌ๋ผ์ด๋ฉ์๋์ฐ)
์ง์ง ์์ฒญ๋๊ฒ ๋ฒ๊ฑฐ๋กญ๊ณ ๋จธ๋ฆฌ๊ฐ ์ ํ ์๋์๊ฐ์ ๋ฆฌ์
ํ๊ณ Array๋ก ๋ง๋ค์ด์ค๋ค์์ ๋ฐฐ์ด[์ซ์]๋ก ์ ๊ทผํ๋ฉด์ ์ฒ๋ฆฌ๋ฅผ ํด์คฌ์ต๋๋ค..!
์์ ๋ฆฌ๋ทฐ์์ ๋ง์๋๋ฆฐ ์ฐจ์ด์ ์ด๋ผํ๋ฉด
1, var cntArr = [Int]()๋ฅผ ์ ์ธํด์ฃผ๊ณ
2, else ์ ๊ฑธ๋ฆฌ๋ฉด (์ฐ์์ด ์๋๋ฉด) cntArr์ ๋ด์์ค๋๋ค.
3. ๋ง์ง๋ง์ print(cntArr.filter { $0 >= time }.map { $0 - time + 1 }.reduce(0, +)) ๋ก ์ฒ๋ฆฌ!
์ฃผ์์ ์จ์๋ ๊ฒ์ฒ๋ผ ์ map์ด ์๊ฒผ๋ํ๋ฉด.. ์ด๊ฒ ๊ฒน์น ์๊ฐ ์๋ค๋ณด๋, ๋ง์ฝ IOIOIOI ๋ฌธ์์ด์ ์ซ์๋ 2๋ผ๋ฉด, ์ด๊ฒ 3๋ฒ ๋ฐ๋ณต๋๋ค๊ณ ํด์ ๋ต์ด 3์ด ๋์ค๊ฒ ์ง๋ง , ๊ทธ๊ฒ ์๋๋ผ 3๋ฒ๋ฐ๋ณต - 2(ํ์์กฐ๊ฑด) + 1 ๋ก ๋ต์ด 2๊ฐ ๋์์ผํ๊ธฐ๋๋ฌธ์ ์์ ์ด ํ์ํ์ต๋๋ค !
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ ๋ฐฑ์ค ๋ถ๋ถ ์ ์์๋ ๊ฑฐ ์ฒ์์์์ด์ ใ
ใ
ใ
๊ธธ์ด ์คํจํ๋ ์ฝ๋๋ ๋น์ทํ ๋ก์ง์ผ๋ก ํ์์ต๋๋ค. ์ํ์ ์ญ์ ํฐ์ง๋๊ตฐ์.
์ฌ๋ผ์ด๋ฉ ์๋์ฐ ์๊ณ ๋ฆฌ์ฆ์ ๋ํด ์ฒ์ ์์๋๋ฐ ํน์ ํจํด์ด ํฌํจ๋ ๋ถ๋ถ ๋ฌธ์์ด ์ฐพ๊ธฐ์ ์ฉ์ดํ ๊ฒ ๊ฐ๊ตฐ์. DP๋ ๋ง์ฐฌ๊ฐ์ง๋ก ์ฝ๋๋ฅผ ๋ณด๊ณ ๋จธ๋ฆฌ์์์ ๋๋ ค๋ดค์ ๋ '์ ์ด๋ ๊ฒ ๋๋ ๊ฑฐ๊ตฌ๋'ํ์ง๋ง, ์ค์ ๋ก ์ง๋ณด๋ผ๊ณ ํ๋ฉด ๋ง๋งํด์ง๋ ๊ฒ ๊ฐ๋ค์. ^^
๊ธธ ์ฝ๋๋ณด๋ฉด ํญ์ ๊น๋ํ ๊ฒ ๊ฐ์์. ํ๋ฒ ์ ์ ๋ฅผ ํ๊ณ PR์ฌ๋ฆฌ์๋์? ์๋๋ฉด ์ด๊ฒ ์ผ์์ ์ฝ๋์ธ๊ฐ์?
(์ฌ๋ผ์ด๋ฉ ์๋์ฐ๋ก๋ ์ํ์ด๋ดค์ต๋๋ค.)
50์ ์ง๋ฆฌ ์ฝ๋
import Foundation
let N = Int(readLine()!)!
let M = Int(readLine()!)!
let S = Array(readLine()!)
var pattern = Array("IOI" + String(repeating: "OI", count: N-1))
let patternCount = pattern.count
var count = 0
for i in 0...M-patternCount {
var isMatch = true
for j in 0..<patternCount {
if S[i+j] != pattern[j] {
isMatch = false
break
}
}
if isMatch {
count += 1
}
}
print(count)
giljihun/๋ฌธ์์ด/[BOJ] IOIOI.swift
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
string์ผ๋ก ๋ฐ๊ณ ๋ฐฐ์ด๋ก ๋ฐ๊ฟ์ฃผ๋ ๊ณผ์ ์์ ๋ณ์๋ฅผ ๋ ์ฐ์ ์ด์ ๊ฐ ์์๊น์?
giljihun/๋ฌธ์์ด/[BOJ] IOIOI.swift
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ณ๊ฑฐ ์๋์ง๋ง ์ด ๋ถ๋ถ ์ ๋ ํน์ดํ๊ฒ ๋๊ปด์ก์ด์! ๋ณดํต ์ผ๋ฐ์ ์ธ ์กฐ๊ฑด๋ฌธ์๋ &&์ฐ์ง ์๋์? Swift์์ ์งํฅํ๋ ์ปจ๋ฒค์
์ธ์ง??
๋ฌผ๋ก ๊ฒฐ๊ณผ๋ ๊ฐ์ง๋ง '์ฝค๋ง'์ '&&'๋ฅผ ์ฐ๋ ๊ธฐ์ค์ด ์์ผ์ ๊ฐ์?
if chars[i - 1] == "I" && chars[i] == "O" && chars[i + 1] == "I"There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์์ด๊ณ .. ๋ฌด ์๋๋๋ค ์นญ์ฐฌ ๋๋ฌด ๊ฐ์ฌํฉ๋๋ค!
1. ํ๋ฒ ์ ์ ๋ฅผ ํ๊ณ PR์ฌ๋ฆฌ์๋์? ์๋๋ฉด ์ด๊ฒ ์ผ์์ ์ฝ๋์ธ๊ฐ์?
์ ์ ํ์ง ์๊ณ ๋ฐฑ์ค์์ ๋ฐ๋ก ๊ธ์ด์ ์ฃผ์๋ง ์์ ํ๊ฑฐ๋? ๊ทธ๋ฐ ๊ฒ ๊ฐ์ต๋๋ค. ๊ทธ๋ฐ๋ฐ ์ ์ด์ ํ ๋ ์์ฒญ ์๊ฐ์ ์ฌ๊ณ ํ์ง ์์์ ๊ทธ๋ฐ ๊ฒ ๊ฐ์์! ใ ใ ์ถ๊ฐ์ ์ผ๋ก๋ ์ด ๋ฌธ์ ๋ ๋ธ๋ฃจํธํฌ์ค๋ก ์คํจํ ๋๋ ๋๊ฒ ๋๋ฝ๊ฑฐ๋ ์! ๊ทธ๋ฅ 100์ ๋ฐ์ ํ์ด๊ฐ ์ ์ด์ ์ฝ๋์ธต์ด ์์์ ๊ทธ๋ ๊ฒ ๋ณด์ด๋ ๊ฒ ๊ฐ์ด๋ค!
2. string์ผ๋ก ๋ฐ๊ณ ๋ฐฐ์ด๋ก ๋ฐ๊ฟ์ฃผ๋ ๊ณผ์ ์์ ๋ณ์๋ฅผ ๋ ์ฐ์ ์ด์ ๊ฐ ์์๊น์?
์ด ์ง๋ฌธ์ด ์ ๊ฐ 2๊ฐ์ง ์๋๋ก ํด์์ด ๋๊ฐ์ง๊ณ , ์ฝ๊ฐ ์ ์ดํด๊ฐ ์๋์๋๋ฐ์!!,,
if 1)
let chars = Array(readLine()!)์ด๋ฐ์์ผ๋ก ์ ํ ๋ฒ์ ๋ฐ์ง ์์๋์ง ์ฌ์ญค๋ณด์๋๊ฑธ๊น์??
์ด๋ ๊ฒ ์์ฑํ ์ด์ ๋, ์ฌ์ค ํฐ ์ด์ ๋ ์๊ณ ๊ฐ๋ ์ฑ์ด ๋ ์ข์ ๋ฐฉ์์ ํํ ๊ฒ ๊ฐ์ต๋๋ค.
if 2)
์๋๋ฉด, ๋ฌธ์์ด์ ์ ๋ ฅ๋ฐ๊ณ ์
let chars = Array(S)๋ฅผ ํตํด Character ๋ฐฐ์ด๋ก ๋ฐ๊พผ ์ด์ ์ ๋ํด ์ฌ์ญค๋ณด์๋๊ฑธ๊น์??
์ด๊ฑด ๋ฌธ์์ด ์ธ๋ฑ์ฑ์ด ๋ถํธํ๊ธฐ ๋๋ฌธ์ ๋๋ค. chars[i]์ฒ๋ผ ์ธ๋ฑ์ค๋ก ๊ฐ ๋ฌธ์๋ฅผ ๋ค๋ฃจ๊ณ ์ถ์ด์ ๋ฐฐ์ดํํ์ต๋๋ค!
3. ์ฝค๋ง'์ '&&'๋ฅผ ์ฐ๋ ๊ธฐ์ค์ด ์์ผ์ ๊ฐ์?
์ค ์ด๊ฑฐ ๋๊ฒ ์ข์ ๊ด์ฐฐ์ด๋ค์.
๊ฒฐ๋ก ์ ์ผ๋ก,
์ ๋ง์ ์คํ์ผ์ด ์๋ค๊ธฐ ๋ณด๋ค๋ '์ฝค๋ง'์ '&' ๋๋ค ๊ฐ์ ๋ฌธ๋ฒ์ด๋ค๋ณด๋,
์กฐ๊ฑด์ ๋ง์ถฐ์ ์์ฐ์ค๋ฌ์ด๊ฒ ๊ด์ฑ์ ์ผ๋ก ๋๊ฐ๋ ๊ฒ ๊ฐ์์.
๊ทผ๋ฐ ์ด๋ ๊ฒ ๋ง์ํด์ฃผ์๊ณ ๋ณด๋,
๊ฐ์ธ์ ์ผ๋ก๋ &&๋ณด๋จ ์ฝค๋ง๊ฐ ๊ฐ๊ฒฐํด์ ๊ฐ๋
์ฑ์ด ๋ ์ข์ ๊ฒ ๊ฐ์ต๋๋ค.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2.
if 1) ์ ์ถ๊ฐ์ ์ผ๋ก
์ ๊ฐ guard let ๊ตฌ๋ฌธ์ผ๋ก ์์ํด๊ฐ์ง๊ตฌ ๋ ์ค๋ก ์จ์ผํ๋ค์!
guard let S = Array(readLine()) else { exit(0) } => โreadLine() -> String?์ด๊ธฐ์
Array(readLine())๋ก ์ธ ์๊ฐ ์๋ค์! (์ต์
๋ ํ์
์ ๋ฐ๋ก ์บ๋ฆญํฐ ๋ฐฐ์ด๋ก ๋ฐ๊พธ๋ คํ๊ธฐ ๋๋ฌธ)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ธ๋ฐ์์ด ๋ฌธ์์ด ์๊ณ ๋ฆฌ์ฆ ์ผ๋ค๊ฐ ๋จธ๋ฆฌํต ๋ฐ์ด๋ ์ ์ ๋๋ค.
- ์ ์ ์ต์ ๋ ์ ์๋ฏธ์ ๊น๋์ฐ์ด์์ต๋๋ค ... ํธํธ
giljihun/๋ฌธ์์ด/[BOJ] IOIOI.swift
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ .... ๋ช๋ฒ ๋์ค๋์ง ๋ฐ๋ณต๋๋๊ฒ IOI์ด๋ ์ด๊ฒ๋ง ๋ฐ๋ณต์ ์ผ๋ก ๊ทธ๋ฆฌ๋ํ๊ฒ ์ฒดํฌํด์ฃผ๋ฉด ๋๋๊ฒ์ด์๊ตฐ์...
์ฒ์์ ์ฌ๋ผ์ด๋ฉ ์๋์ฐ + ๋ธ๋ฃจํธ ํฌ์ค๋ก ํ์์๋ ์๊ฐ์ด๊ณผ๋ก ๋ปฅ ! ํ๊ณ ํฐ์ ธ์ ํ๋ถ๋ ๋ฐฐ์ด๊ฒ ์๊ฐ๋์ ์์ ์ง์์ผ๋ก KMP๋ฅผ ๊ฒ์ํ์ฌ ๊ตฌํํ์ฌ ๋ง๋ค์ด๋ณด์๋๋ฐ.... ์ดํดํ๋ ์๊ฐ์ด ์์ฒญ ์ค๋ ๊ฑธ๋ ธ์ต๋๋ค...
์๊ฐ๊ณผ ๊ณต๊ฐ์ด ์ ์ผ ์ข์๊ฒ ์ ค ์์๊ฐ ๊ธธ ์ฝ๋์
๋๋ค ใ
ใ
ใ
๋ฐ๋ณต์ด ๋ณด์ฅ๋ ๊ณณ์์๋ ๊ทธ๋ฆฌ๋ํ๊ฒ ํ๋ฉด๋๋ ๋ฌธ์ ์
๋๋ค... ใ
ใ
ใ
...
์๋๋ KMP ์๊ณ ๋ฆฌ์ฆ์ผ๋ก ์ ๋ฏธ์ฌ, ์ ๋์ฌ๋ก ์ด๋๊น์ง ๋ฐ๋ณต๋๋์ง pi ๋ฐฐ์ด์ ๋ด์์ intํ์ผ๋ก ๊ณ์ฐํ ํ KMP๋ฅผ ํตํด ์์ชฝ ๋์์ ๋ถํฐ ๋ธ๋ฃจํธ ํฌ์ค์ฒ๋ผ ์ผ์ผ์ด ๋ค ์ฒดํฌํ๋๊ฒ์ด์๋๋ผ ์์ชฝ์์ ์ขํ์ค๋ฉด์ ์ผ์นํ๋ค๊ฐ ํ๋๊ฐ ํ๋ฆฐ ํจํด์ด ๋์ค๋ฉด ๊ทธ๋งํผ ๋์ด๋๊ณ ๊ทธ ๋ค์ ๋ถํฐ ์ฒดํฌํ๋ ์์ฒญ ํจ์จ์ ์ธ ๋ฌธ์์ด ์๊ณ ๋ฆฌ์ฆ์ ๋๋ค.
KMP ๋ธ๋ก๊ทธ
https://velog.io/@hwan2da/%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98-KMP-%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98
KMP ์์
https://www.youtube.com/watch?v=yWWbLrV4PZ8
import Foundation
// N = 'O' ์ ๊ฐ์ N+1 = 'I'์ ๊ฐ์
// PN์ ๊ธธ์ด๋ 2N+1 -> I๋ง ๋ถ์ด๊ณ ๋ค์ OI๋ง ๋ถ์ด๋ฉด ?
// ์ฌ๋ผ์ด๋ฉ ์๋์ฐ์ KMP ์ฌ์ฉํ๋ฉด ๋ ๋ฏํจ ?
// ์ต์ N์ 1์ด์์
// 1. ํจํด ๋ง๋ค๊ธฐ: "IOI"๊ฐ N๋ฒ ๋ฐ๋ณต๋ PN
func makePattern(n: Int) -> String {
var pattern = "I"
for _ in 0..<n {
pattern += "OI"
}
return pattern
}
// 2. ์ ๋์ฌ ๋ฐฐ์ด (pi ๋ฐฐ์ด) ์์ฑ
func makePi(_ pattern: String) -> [Int] {
let p = Array(pattern)
var pi = Array(repeating: 0, count: p.count)
var j = 0
for i in 1..<p.count {
while j > 0 && p[i] != p[j] {
j = pi[j - 1]
}
if p[i] == p[j] {
j += 1
pi[i] = j
}
}
return pi
}
// 3. KMP ๋งค์นญ
func kmp(_ text: String, _ pattern: String) -> Int {
let t = Array(text)
let p = Array(pattern)
let pi = makePi(pattern)
var j = 0
var count = 0
for i in 0..<t.count {
while j > 0 && t[i] != p[j] {
j = pi[j - 1]
}
if t[i] == p[j] {
if j == p.count - 1 {
count += 1
j = pi[j]
} else {
j += 1
}
}
}
return count
}
// 4. ๋ฉ์ธ ์คํ
let N = Int(readLine()!)!
let _ = Int(readLine()!)! // S์ ๊ธธ์ด๋ ์ ์จ๋ ๋จ
let S = readLine()!
let pattern = makePattern(n: N)
let result = kmp(S, pattern)
print(result)
|
์ ๋ ๋์ ํ ํ ๋ฐฉ๋ฒ์ด ์๊ฐ ์ ๋์ ๋ฐ๋ฏธ์ ์ฝ๋๋ ๊ธธ ์ฝ๋ ์ดํดํ๋ฉด์ ์ ๊ฐ ๋ค์ ์ง๋ดค์ด์ ! //
// main.swift
// 5525
//
// Created by Seungeun Park on 5/7/25.
//
import Foundation
let N = Int(readLine()!)!
let _ = Int(readLine()!)! // ๋ฌธ์์ด ๊ธธ์ด๋ ํ์ ์์
let S = Array(readLine()!) // ๋ฌธ์์ด์ ๋ฐฐ์ด๋ก ๋ฐ๊ฟ ์ธ๋ฑ์ฑํ๊ธฐ ์ฝ๊ฒ
var i = 0
var match = 0
var count = 0
while i < S.count - 2 {
if S[i] == "I" && S[i+1] == "O" && S[i+2] == "I" {
match += 1
if match == N {
count += 1
match -= 1 // ์ค์ฒฉ ๊ฐ๋ฅํ ํจํด ๊ณ ๋ ค
}
i += 2 // IOI ๋ค์์ 'I'๋ถํฐ ๋ค์ ๊ฒ์ฌ
} else {
match = 0
i += 1
}
}
print(count) |
alstjr7437
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ค๋ฅธ๋ถ๋ค์ด ์ด๋ฏธ ๋ฆฌ๋ทฐ๋ฅผ ๋๋ฌด ์ํด๋์ฃผ์ ์ ๋ ํ ๋ง์ด ์๋ค์!!...
์ ๋ ์๋์ ๊ฐ์ด ํ์์ต๋๋ค!
let n = Int(readLine()!)!
let m = Int(readLine()!)!
let s = readLine()!.map{$0}
var result = 0
var cur = 0
var count = 0
while cur < m-2{
if String(s[cur...cur + 2]) == "IOI"{
count += 1
cur += 2
if count == n{
result += 1
count -= 1
}
} else {
count = 0
cur += 1
}
}
print(result)
giljihun/๋ฌธ์์ด/[BOJ] IOIOI.swift
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
index์ฒ๋ผ ๋ณ์๋ช ์ ์ฌ์ฉํด๋ ๊ด์ฐฎ์์ ๊ฒ ๊ฐ์๋ฐ i๋ก ์ฌ์ฉํ์ ์ด์ ๋ ๊ฐ๊ฒฐ์ฑ ๋๋ฌธ์ผ๊น์?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ง์ด๋ค ์กฐ๊ฑด์ด ๋จ์ํ๊ธดํด๋ ๊ธธ์ด์ ์ด๋๋ ๊ฐ๊ฒฐ์ฑ์ด ๋ ์ค์ํ๋ค๊ณ ํ๋จํ์ด๋ค!

๐ ๋ฌธ์ ๋งํฌ
๋ฐฑ์ค - IOIOI
โ๏ธ ์์๋ ์๊ฐ
1h
โจ ์๋ ์ฝ๋
ํ๋ฆฐ ์ ๊ทผ
์ผ๋จ ๋จผ์ ํ๋ฆฐ ์ ๊ทผ ๋จผ์ ๋ง์๋๋ฆฌ๊ฒ ์ต๋๋ค.
์์ ์ฌ์ง ๋ณด๋ฉด ๋ณด์ด์๊ฒ ์ง๋ง, ์๊ฐ ์ด๊ณผ ์ผ๋ฌด์ง๊ฒ ํ์ต๋๋ค !
๊ทผ๋ฐ ๋ญ. ๊ตฌํ์ ์ผ๋จ ๋ฐ๊ณ ๋ณด๋๊ฑฐ ์๋๊ฒ ์ผ์(์ด๊ฑฐ ๋ง์ต๋๊บผ ๋ฐ๋ฏธ์)
์ ๋ ฅ์ผ๋ก ์ฃผ์ด์ง๋ "ํจํด"์ ๋จผ์ ๋ง๋ ๋ค.
์๋ฅผ๋ค์ด N = 2, pattern = "IOIOI".
๋ฌธ์์ด ์ธ๋ฑ์ค ์์์ , ๋์ ์ 1์ฉ ์ฆ๊ฐ์ํค๋ฉฐ ๋๊น์ง ๋๋ค.
(๋์ ์ด ์ด๊ณผ๋์ง ์์๋๊น์ง)
ํ์ง๋ง ์ด ๋ฐฉ์์ !
์ด์ ๋ ๋๊ฐ์ง๋ก ํ์ธ์ด ๋ฉ๋๋ค.
๊ฒฐ๋ก ์ ์ผ๋ก ํจํด ์ฌ๋ผ์ด์ฑ + ๋ธ๋ฃจํธํฌ์ค๋ก ์๊ฐ ๋ถ๋ถ !
์ ์๋ฌ๋ฅผ ํตํด ์ฐพ์์ผํ ๋ฐฉ๋ฒ์?
๋น์ฅ ๋ง ๋ ์ค๋ฅด์ง ์์์์. ๊ทธ๋ ์ง๋ง ํ์คํ ๊ฒ์ ์๋ค.
"์ต๋ M๋ฒ๋ง ๋น๊ตํ๋๋ก ๋ง๋ค์ด์ผํ๋ค."
= "ํ ๋ฒ์ ์ค์บ๋ง ํ๋๋ก ๋ก์ง์ ๋ฐ๊ฟ์ผํ๋ค."
์ ๋ต์ ๊ฐ์ ธ๋ค์ค ์ ๊ทผ
N: 1-> "IOI",
N: 2 -> "IOIOI",
N: 3 -> "IOIOIOI".
๊ฒฐ๊ตญ, "IOI"๊ฐ ๋ฐ๋ณต๋๋ค๋ ๊ฒ.
๊ทธ๋ฌ๋๊น, N์ด 2๋ฉด.
IOI ๋ค์์ ๋ง์ง๋ง I์ ์ด์ด์ OI๊ฐ ์ค๋ฉด ์ฐ๋ฆฌ๊ฐ ์ํ๋ ํจํด์ด๋ผ๋ ๊ฒ!
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ
1. ๋ฌธ์์ด ํ์ ์ต์ ํ์ ์ค์์ฑ
2. ์บ๋ฆญํฐ ๋ฐฐ์ด๋ก ๋ณํํด์ ์ธ๋ฑ์ค ์ ๊ทผํ๊ธฐ
์ ๋ ๋ฌธ์์ด์ ์บ๋ฆญํฐ ๋ฐฐ์ด๋ก ๋ณํํด์ ์ฌ์ฉํ๋๋ฐ์.
์ด๋ฐ ๋ฌธ์์ด ๋ฌธ์ ๋ฅผ ์๋ก์ด ๋ฐฉํฅ์ฑ์ ์ฐพ๊ธฐ ์ข์๋ ๊ฒ ๊ฐ์ต๋๋ค.
(์ฌ์ค ์ด๊ฒ ๋๊ฒ key์๋ ๊ฒ ๊ฐ์์.)
3. ๋ฌธ์์ด ํ์ ๋ฌธ์ ์์์ ์๊ฐ๋ณต์ก๋
ํ๊ธฐ
์นดํก์ธ๊ฐ ์ด๋๊ฐ์์ "๋ฌธ์์ด ํ์" ํธ์ จ๋ค๊ณ ๋ค์ด์..
์๊ฐํด๋ณด๋ ์ฌ๋ผ์ด์ฑ์ด ํ์ด์ฌํ๊ณ ๋ ํ์คํ ๋ฌ๋๋ ๊ฒ ๊ฐ๊ณ ํด์
๋ค์ ์ฌ๋ผ์ด์ฑ์ ๋ํด ๊ณต๋ถ๊ฒธ ํ์ด๋ดค๋๋ฐ.
๊ฑ ํ์คํ Swift์์ ๋ฌธ์์ด ์ฌ๋ผ์ด์ฑ์ ์ฐ๋ ๊ธฐ๊ฐ ๋ง๋๊ฑธ๋ก..
(์๋๋ฉด ๋ฐ๋ฐ์ข ํด์ฃผ์ธ์ ์ ๋ฐ. ์ ๋ ์๋๋ผ๊ณ ํ๊ณ ์ถ์ด์.)