-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import Foundation | ||
|
|
||
| // MARK: - Input | ||
| guard let N = Int(readLine() ?? "") else { exit(0) } | ||
| guard let M = Int(readLine() ?? "") else { exit(0) } | ||
| guard let S = readLine() else { exit(0) } | ||
|
|
||
| let chars = Array(S) | ||
|
Comment on lines
+6
to
+8
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. string์ผ๋ก ๋ฐ๊ณ ๋ฐฐ์ด๋ก ๋ฐ๊ฟ์ฃผ๋ ๊ณผ์ ์์ ๋ณ์๋ฅผ ๋ ์ฐ์ ์ด์ ๊ฐ ์์๊น์? |
||
| var result = 0 | ||
| var count = 0 | ||
|
|
||
| // ํ์ฌ ์ธ๋ฑ์ค ์์น | ||
| var i = 1 | ||
|
|
||
| while i < M - 1 { | ||
| // IOI๊ฐ ๋ฑ์ฅ? -> ์นด์ดํธ๋ฅผ 1๊ฐ ์ฌ๋ฆผ | ||
| if chars[i - 1] == "I", chars[i] == "O", chars[i + 1] == "I" { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ์์ด๊ณ .. ๋ฌด ์๋๋๋ค ์นญ์ฐฌ ๋๋ฌด ๊ฐ์ฌํฉ๋๋ค! 1. ํ๋ฒ ์ ์ ๋ฅผ ํ๊ณ PR์ฌ๋ฆฌ์๋์? ์๋๋ฉด ์ด๊ฒ ์ผ์์ ์ฝ๋์ธ๊ฐ์?
2. string์ผ๋ก ๋ฐ๊ณ ๋ฐฐ์ด๋ก ๋ฐ๊ฟ์ฃผ๋ ๊ณผ์ ์์ ๋ณ์๋ฅผ ๋ ์ฐ์ ์ด์ ๊ฐ ์์๊น์?์ด ์ง๋ฌธ์ด ์ ๊ฐ 2๊ฐ์ง ์๋๋ก ํด์์ด ๋๊ฐ์ง๊ณ , ์ฝ๊ฐ ์ ์ดํด๊ฐ ์๋์๋๋ฐ์!!,, if 1)let chars = Array(readLine()!)์ด๋ฐ์์ผ๋ก ์ ํ ๋ฒ์ ๋ฐ์ง ์์๋์ง ์ฌ์ญค๋ณด์๋๊ฑธ๊น์??
if 2)์๋๋ฉด, ๋ฌธ์์ด์ ์ ๋ ฅ๋ฐ๊ณ ์ let chars = Array(S)๋ฅผ ํตํด Character ๋ฐฐ์ด๋ก ๋ฐ๊พผ ์ด์ ์ ๋ํด ์ฌ์ญค๋ณด์๋๊ฑธ๊น์??
3. ์ฝค๋ง'์ '&&'๋ฅผ ์ฐ๋ ๊ธฐ์ค์ด ์์ผ์ ๊ฐ์?์ค ์ด๊ฑฐ ๋๊ฒ ์ข์ ๊ด์ฐฐ์ด๋ค์. ๊ฒฐ๋ก ์ ์ผ๋ก, ๊ทผ๋ฐ ์ด๋ ๊ฒ ๋ง์ํด์ฃผ์๊ณ ๋ณด๋,
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 2.if 1) ์ ์ถ๊ฐ์ ์ผ๋ก guard let S = Array(readLine()) else { exit(0) } => โreadLine() -> String?์ด๊ธฐ์
Comment on lines
+12
to
+17
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. index์ฒ๋ผ ๋ณ์๋ช ์ ์ฌ์ฉํด๋ ๊ด์ฐฎ์์ ๊ฒ ๊ฐ์๋ฐ i๋ก ์ฌ์ฉํ์ ์ด์ ๋ ๊ฐ๊ฒฐ์ฑ ๋๋ฌธ์ผ๊น์?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๋ง์ด๋ค ์กฐ๊ฑด์ด ๋จ์ํ๊ธดํด๋ ๊ธธ์ด์ ์ด๋๋ ๊ฐ๊ฒฐ์ฑ์ด ๋ ์ค์ํ๋ค๊ณ ํ๋จํ์ด๋ค! |
||
| count += 1 | ||
| // ์นด์ดํธ๊ฐ ๋ง์ฝ N์ด ๋์๋ค? ์ฐ๋ฆฌ๊ฐ ์ฐพ๋ ํจํด์ด ๋ฑ์ฅํ ๊ฒ. | ||
| if count == N { | ||
| result += 1 | ||
| // ๊ทผ๋ฐ ๋ฐ๋ก ๋ค์์ ์ค๋ณต์ ์ผ๋ก ํจํด์ด ๋์ฌ ์๋ ์์ผ๋, 1์ ์ค์ด๊ธฐ. | ||
| count -= 1 | ||
|
Comment on lines
+20
to
+23
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ์ด ๋ถ๋ถ๋ง ์ด์ง๋ค๋ฅด๋ค์! ์ ๋ Array์ ๋ด์์คฌ๋๋ฐ, ์ด๋ ๊ฒํ๋ฉด ๋์ค์ filter,map,reduce์ฐ๋ฉด์ ์ฒ๋ฆฌํด์ฃผ๋๋ผ ์๊ฐ,๋ฉ๋ชจ๋ฆฌ ๋ค ์ข ์ํด๋ณด๋ ๋๋์ด์๊ฑฐ๋ ์..
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๊ฐ์ฌํฉ๋๋ค ใ
ใ
์ง๊ธ ๊ตฌ์กฐ๊ฐ ํจํด์ ์์ง -> ํ์ฒ๋ฆฌํ๋ ๊ตฌ์กฐ์ธ๋ฐ,
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ์๋ ์ฝ๋๋ ํ์ธํด๋ณด๋ ํ์ฒ๋ฆฌ๋ฅผ ํด๋ก์ ๋ก ํ ๋ฒ์ ์กฐ์ง์๋(?) ๋ถ๋ถ์ด ์ผ๋ฌด์ง๋ค์.. |
||
| } | ||
| i += 2 | ||
| } | ||
| // ๋ค์์ผ๋ก ์ด๋ํ๋๋ IOI๊ฐ ์๋์ด? -> 0์ผ๋ก ์ด๊ธฐํ. | ||
| // ๊ทธ๋ฆฌ๊ณ ๋ค์ ์ธ๋ฑ์ค๋ถํฐ ๋ค์ ๋น๊ต์์. | ||
| else { | ||
| count = 0 | ||
| i += 1 | ||
| } | ||
| } | ||
| print(result) | ||
|
Comment on lines
+15
to
+34
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ์ .... ๋ช๋ฒ ๋์ค๋์ง ๋ฐ๋ณต๋๋๊ฒ IOI์ด๋ ์ด๊ฒ๋ง ๋ฐ๋ณต์ ์ผ๋ก ๊ทธ๋ฆฌ๋ํ๊ฒ ์ฒดํฌํด์ฃผ๋ฉด ๋๋๊ฒ์ด์๊ตฐ์... ์ฒ์์ ์ฌ๋ผ์ด๋ฉ ์๋์ฐ + ๋ธ๋ฃจํธ ํฌ์ค๋ก ํ์์๋ ์๊ฐ์ด๊ณผ๋ก ๋ปฅ ! ํ๊ณ ํฐ์ ธ์ ํ๋ถ๋ ๋ฐฐ์ด๊ฒ ์๊ฐ๋์ ์์ ์ง์์ผ๋ก KMP๋ฅผ ๊ฒ์ํ์ฌ ๊ตฌํํ์ฌ ๋ง๋ค์ด๋ณด์๋๋ฐ.... ์ดํดํ๋ ์๊ฐ์ด ์์ฒญ ์ค๋ ๊ฑธ๋ ธ์ต๋๋ค... ์๊ฐ๊ณผ ๊ณต๊ฐ์ด ์ ์ผ ์ข์๊ฒ ์ ค ์์๊ฐ ๊ธธ ์ฝ๋์
๋๋ค ใ
ใ
ใ
์๋๋ KMP ์๊ณ ๋ฆฌ์ฆ์ผ๋ก ์ ๋ฏธ์ฌ, ์ ๋์ฌ๋ก ์ด๋๊น์ง ๋ฐ๋ณต๋๋์ง pi ๋ฐฐ์ด์ ๋ด์์ intํ์ผ๋ก ๊ณ์ฐํ ํ KMP๋ฅผ ํตํด ์์ชฝ ๋์์ ๋ถํฐ ๋ธ๋ฃจํธ ํฌ์ค์ฒ๋ผ ์ผ์ผ์ด ๋ค ์ฒดํฌํ๋๊ฒ์ด์๋๋ผ ์์ชฝ์์ ์ขํ์ค๋ฉด์ ์ผ์นํ๋ค๊ฐ ํ๋๊ฐ ํ๋ฆฐ ํจํด์ด ๋์ค๋ฉด ๊ทธ๋งํผ ๋์ด๋๊ณ ๊ทธ ๋ค์ ๋ถํฐ ์ฒดํฌํ๋ ์์ฒญ ํจ์จ์ ์ธ ๋ฌธ์์ด ์๊ณ ๋ฆฌ์ฆ์ ๋๋ค. KMP ๋ธ๋ก๊ทธ 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)
|
||

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.
์ ๋ ฅ๋ฐ๋ ๋ถ๋ถ ์ ํ์ฉํ์ต๋๋ค.. ๐