Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions giljihun/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
|:----:|:---------:|:----:|:-----:|:----:|
| 1์ฐจ์‹œ | 2025.03.30 | DP | [1๋กœ ๋งŒ๋“ค๊ธฐ](https://www.acmicpc.net/problem/1463)|https://github.com/AlgoLeadMe/AlgoLeadMe-14/pull/4|
| 2์ฐจ์‹œ | 2025.04.08 | DP | [๋™์ „ 1](https://www.acmicpc.net/problem/2293)|https://github.com/AlgoLeadMe/AlgoLeadMe-14/pull/8|
| 3์ฐจ์‹œ | 2025.04.30 | ๋ฌธ์ž์—ด | [IOIOI](https://www.acmicpc.net/problem/5525)|https://github.com/AlgoLeadMe/AlgoLeadMe-14/pull/15|
---
34 changes: 34 additions & 0 deletions giljihun/๋ฌธ์ž์—ด/[BOJ] IOIOI.swift
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) }
Comment on lines +4 to +6
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ž…๋ ฅ๋ฐ›๋Š” ๋ถ€๋ถ„ ์ž˜ ํ™œ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.. ๐Ÿ‘


let chars = Array(S)
Comment on lines +6 to +8
Copy link
Collaborator

Choose a reason for hiding this comment

The 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" {
Copy link
Collaborator

@MuchanKim MuchanKim May 4, 2025

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"

Copy link
Member Author

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. ์ฝค๋งˆ'์™€ '&&'๋ฅผ ์“ฐ๋Š” ๊ธฐ์ค€์ด ์žˆ์œผ์‹ ๊ฐ€์š”?

์˜ค ์ด๊ฑฐ ๋˜๊ฒŒ ์ข‹์€ ๊ด€์ฐฐ์ด๋„ค์š”.

๊ฒฐ๋ก ์ ์œผ๋กœ,
์ €๋งŒ์˜ ์Šคํƒ€์ผ์ด ์žˆ๋‹ค๊ธฐ ๋ณด๋‹ค๋Š” '์ฝค๋งˆ'์™€ '&' ๋‘˜๋‹ค ๊ฐ™์€ ๋ฌธ๋ฒ•์ด๋‹ค๋ณด๋‹ˆ,
์กฐ๊ฑด์— ๋งž์ถฐ์„œ ์ž์—ฐ์Šค๋Ÿฌ์šด๊ฒŒ ๊ด€์„ฑ์ ์œผ๋กœ ๋‚˜๊ฐ€๋Š” ๊ฒƒ ๊ฐ™์•„์š”.

๊ทผ๋ฐ ์ด๋ ‡๊ฒŒ ๋ง์”€ํ•ด์ฃผ์‹œ๊ณ  ๋ณด๋‹ˆ,
๊ฐœ์ธ์ ์œผ๋กœ๋Š” &&๋ณด๋‹จ ์ฝค๋งˆ๊ฐ€ ๊ฐ„๊ฒฐํ•ด์„œ ๊ฐ€๋…์„ฑ์ด ๋” ์ข‹์€ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

Copy link
Member Author

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())๋กœ ์“ธ ์ˆ˜๊ฐ€ ์—†๋„ค์š”! (์˜ต์…”๋„ ํƒ€์ž…์„ ๋ฐ”๋กœ ์บ๋ฆญํ„ฐ ๋ฐฐ์—ด๋กœ ๋ฐ”๊พธ๋ คํ•˜๊ธฐ ๋•Œ๋ฌธ)

Comment on lines +12 to +17
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

index์ฒ˜๋Ÿผ ๋ณ€์ˆ˜๋ช…์„ ์‚ฌ์šฉํ•ด๋„ ๊ดœ์ฐฎ์•˜์„ ๊ฒƒ ๊ฐ™์€๋ฐ i๋กœ ์‚ฌ์šฉํ•˜์‹  ์ด์œ ๋Š” ๊ฐ„๊ฒฐ์„ฑ ๋•Œ๋ฌธ์ผ๊นŒ์š”?

Copy link
Member Author

Choose a reason for hiding this comment

The 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ด ๋ถ€๋ถ„๋งŒ ์‚ด์ง๋‹ค๋ฅด๋„ค์š”! ์ €๋Š” Array์— ๋‹ด์•„์คฌ๋Š”๋ฐ, ์ด๋ ‡๊ฒŒํ•˜๋ฉด ๋‚˜์ค‘์— filter,map,reduce์“ฐ๋ฉด์„œ ์ฒ˜๋ฆฌํ•ด์ฃผ๋А๋ผ ์‹œ๊ฐ„,๋ฉ”๋ชจ๋ฆฌ ๋‹ค ์ข€ ์†ํ•ด๋ณด๋Š” ๋А๋‚Œ์ด์—ˆ๊ฑฐ๋“ ์š”..
๊ธธ์„ ์ƒ๋‹˜์ด ํ‘ธ์‹ ๊ฒŒ ๋” ์ง๊ด€์ ์ด๊ณ  ๋ฉ”๋ชจ๋ฆฌ์—๋„ ํšจ์œจ์ ์ธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค..!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค ใ…‹ใ…‹
์Œ Array์— ๋‹ด์•„์„œ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ์‹์ด ๊ฐ€๋…์„ฑ์€ ๋” ์ข‹์„ ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š”

์ง€๊ธˆ ๊ตฌ์กฐ๊ฐ€ ํŒจํ„ด์„ ์ˆ˜์ง‘ -> ํ›„์ฒ˜๋ฆฌํ•˜๋Š” ๊ตฌ์กฐ์ธ๋ฐ,
์ค‘๊ฐ„ ๋กœ์ง์ด ๋ณต์žกํ•˜๋‹ค๋ฉด ์‚ฌ์‹ค ์ œ๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ๋กœ๋Š” ํ๋ฆ„์„ ๋ถ„๋ฆฌํ•˜๊ณ  ํ…Œ์ŠคํŒ…ํ•˜๊ธฐ์—” ์–ด๋ ค์šธ ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ฉ”๋ชจ๋ฆฌ ์†ํ•ด์— ๋Œ€ํ•œ ๋‚ด์šฉ์€ ์ •ํ™•ํ•œ ์บ์น˜๋ฅผ ํ•˜์‹  ๊ฒƒ ๊ฐ™๋„ค์š”!! ๊ตฟ์ž…๋‹ˆ๋‹ค

Copy link
Member Author

Choose a reason for hiding this comment

The 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์Œ .... ๋ช‡๋ฒˆ ๋‚˜์˜ค๋Š”์ง€ ๋ฐ˜๋ณต๋˜๋Š”๊ฒŒ IOI์ด๋‹ˆ ์ด๊ฒƒ๋งŒ ๋ฐ˜๋ณต์ ์œผ๋กœ ๊ทธ๋ฆฌ๋””ํ•˜๊ฒŒ ์ฒดํฌํ•ด์ฃผ๋ฉด ๋˜๋Š”๊ฒƒ์ด์—ˆ๊ตฐ์š”...

์ฒ˜์Œ์— ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ + ๋ธŒ๋ฃจํŠธ ํฌ์Šค๋กœ ํ’€์—ˆ์„๋•Œ ์‹œ๊ฐ„์ดˆ๊ณผ๋กœ ๋ปฅ ! ํ•˜๊ณ  ํ„ฐ์ ธ์„œ ํ•™๋ถ€๋•Œ ๋ฐฐ์šด๊ฒŒ ์ƒ๊ฐ๋‚˜์„œ ์–•์€ ์ง€์‹์œผ๋กœ KMP๋ฅผ ๊ฒ€์ƒ‰ํ•˜์—ฌ ๊ตฌํ˜„ํ•˜์—ฌ ๋งŒ๋“ค์–ด๋ณด์•˜๋Š”๋ฐ.... ์ดํ•ดํ•˜๋Š” ์‹œ๊ฐ„์ด ์—„์ฒญ ์˜ค๋ž˜ ๊ฑธ๋ ธ์Šต๋‹ˆ๋‹ค...

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2025-05-06 แ„‹แ…ฉแ„’แ…ฎ 10 06 23

์‹œ๊ฐ„๊ณผ ๊ณต๊ฐ„์ด ์ œ์ผ ์ข‹์€๊ฒŒ ์ ค ์œ„์—๊ฐ€ ๊ธธ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค ใ…‹ใ…‹ใ…‹
๋ฐ˜๋ณต์ด ๋ณด์žฅ๋œ ๊ณณ์—์„œ๋Š” ๊ทธ๋ฆฌ๋””ํ•˜๊ฒŒ ํ’€๋ฉด๋˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค... ใ…‹ใ…‹ใ…‹...

์•„๋ž˜๋Š” 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)