Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/Baekjoon

[๋ฐฑ์ค€] (Swift) 20291๋ฒˆ - ํŒŒ์ผ ์ •๋ฆฌ (๊ตฌํ˜„)

๊ฐ์ž ๐Ÿฅ” 2023. 2. 15. 22:12
๋ฐ˜์‘ํ˜•

๐ŸŸ  ๋ฌธ์ œ

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

 

20291๋ฒˆ: ํŒŒ์ผ ์ •๋ฆฌ

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

www.acmicpc.net

 

๐ŸŸ  ๋‚˜์˜ ํ’€์ด

๋ฌธ์ œ๋Š” ๋ญ ์ฃผ์ ˆ์ฃผ์ ˆ ๊ธธ์ง€๋งŒ, ํ™•์žฅ์ž๋ณ„๋กœ ๊ฐฏ์ˆ˜๋ฅผ ์„ธ์–ด์ฃผ๊ณ , ํ™•์žฅ์ž๋ช… ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ถœ๋ ฅํ•ด์ฃผ๋ฉด ๋˜๋Š” ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ๋‹ค. ๋‚˜๋Š” ํŠน์ • ํ™•์žฅ์ž๋ณ„๋กœ ๊ฐฏ์ˆ˜๋ฅผ ์„ธ์–ด์ฃผ๋Š” ๋ฐฉ์‹์„ dictionary๋กœ ๊ตฌํ˜„ํ–ˆ๋‹ค.

dict์•ˆ์— ๋‚ด๊ฐ€ ์›ํ•˜๋Š” ํ™•์žฅ์ž๋ช…์ด ์žˆ๋‹ค๋ฉด value์— + 1์„ ํ•ด์ฃผ์—ˆ๊ณ , ์—†๋‹ค๋ฉด dict์— ์ƒˆ๋กœ ์ƒ์„ฑํ•ด์ฃผ๋Š” ๋ฐฉ์‹์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  dict๋ฅผ key๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•ด์ค€๋’ค, key์™€ value๋ฅผ ์ถœ๋ ฅํ•ด์ฃผ๋ฉด ๋!

๐ŸŸ  ์ •๋‹ต ์ฝ”๋“œ

import Foundation

let n = Int(readLine()!)!
var dict = [String:Int]()

for _ in 0..<n {
    let temp = readLine()!.split(separator: ".").map{ String($0) }

    if !dict.keys.contains(temp[1]) {
        dict[temp[1]] = 1
    } else {
        dict[temp[1]]! += 1
    }
}

let sortedDict = dict.sorted { $0.0 < $1.0 }
for i in 0..<sortedDict.count {
    print("\(sortedDict[i].key) \(sortedDict[i].value)")
}
๋ฐ˜์‘ํ˜•