๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
3๏ธโƒฃ Swift/Problem Solving

[Swift Algorithm] ์•ŒํŒŒ๋ฒณ ๊ฐœ์ˆ˜ BOJ #10808

by seolhee2750 2021. 7. 27.
๋ฌธ์ œ ์„ค๋ช…

์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ๋‹จ์–ด S๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.

๊ฐ ์•ŒํŒŒ๋ฒณ์ด ๋‹จ์–ด์— ๋ช‡ ๊ฐœ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

 

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ๋‹จ์–ด S๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‹จ์–ด์˜ ๊ธธ์ด๋Š” 100์„ ๋„˜์ง€ ์•Š์œผ๋ฉฐ, ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค.

 

์ถœ๋ ฅ

๋‹จ์–ด์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” a์˜ ๊ฐœ์ˆ˜, b์˜ ๊ฐœ์ˆ˜, …, z์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•ด์„œ ์ถœ๋ ฅํ•œ๋‹ค.

 

์ž…์ถœ๋ ฅ ์˜ˆ์ œ

์ž…๋ ฅ

baekjoon

์ถœ๋ ฅ

1 1 0 0 1 0 0 0 0 1 1 0 0 1 2 0 0 0 0 0 0 0 0 0 0 0

 

๋‚ด ๋ฌธ์ œ ํ’€์ด
import Foundation

let alphabet = Array("abcdefghijklmnopqrstuvwxyz")
var result = Array(repeating: "0", count: 26)
var inputStr = readLine()!
var check = [String]()

for i in inputStr {
    if !check.contains(String(i)) {
        var temp = 0
        for j in inputStr {
            if i == j { temp += 1 }
        }
        result[alphabet.firstIndex(of: i)!] = String(temp)
    }
    check.append(String(i))
}

print(result.joined(separator: " "))
  • ์†Œ๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•ด์ฃผ์—ˆ๊ณ , ์•ŒํŒŒ๋ฒณ ๊ฐœ์ˆ˜๋งŒํผ "0"์œผ๋กœ ์ฑ„์šด ๋ฐฐ์—ด๋„ ๋งŒ๋“ค์—ˆ๋‹ค.
  • ์ž…๋ ฅ๋ฐ›์€ inputStr์˜ ๊ธ€์ž๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ๊ฒ€์‚ฌํ•˜๋ฉฐ alphabet ๋ฐฐ์—ด์—์„œ ํ•ด๋‹น ๊ธ€์ž์˜ ์œ„์น˜๋ฅผ ์ฐพ๊ณ ,
    ์ด์ค‘ for๋ฌธ์„ ์ด์šฉํ•ด ํ•ด๋‹น ๊ธ€์ž๊ฐ€ inputStr์— ๋ช‡ ๊ฐœ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•˜์—ฌ
    result ๋ฐฐ์—ด์˜ ๊ฐ™์€ ์œ„์น˜์— 1์„ ์ถ”๊ฐ€ํ•ด์ฃผ๋Š” ์‹์œผ๋กœ ๊ตฌ์„ฑํ–ˆ๋‹ค.

 

๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ
  • ์ฒ˜์Œ๋ถ€ํ„ฐ result ๋ฐฐ์—ด์„ Intํ˜•์œผ๋กœ ์ƒ์„ฑํ•˜๊ณ , ๊ตณ์ด check ๋ฐฐ์—ด ์‚ฌ์šฉํ•  ํ•„์š” ์—†์ด
    ์ˆœ์„œ๋Œ€๋กœ ํ•ด๋‹นํ•˜๋Š” ์•ŒํŒŒ๋ฒณ ์œ„์น˜์— 1์”ฉ ๋”ํ•ด์คฌ์œผ๋ฉด ๋” ๊น”๋”ํ–ˆ์„ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค.
  • ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค๋งŒ ํ’€๋‹ค๊ฐ€ ๋ฐฑ์ค€์„ ์ฒ˜์Œ ์‹œ์ž‘ํ–ˆ๋Š”๋ฐ, ์ž…๋ ฅ๋ฐ›๋Š” ๋ฐฉ์‹๋„ ๋‹ค์–‘ํ•˜๊ฒŒ ์ƒ๊ฐํ•˜๊ฒŒ ๋˜๋‹ˆ ์ข‹์€ ๊ฒƒ ๊ฐ™๋‹ค.

 


 

๋ฌธ์ œ

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

 

๋Œ“๊ธ€