๊ธ€ ์ž‘์„ฑ์ž: ์ด์ง€์›๐ŸŒฉ๏ธ

๋‘ ๊ทธ๋ฃน์„ ๋”ํ•  ๋•Œ, ํŠœํ”Œ์„ ์ด์šฉํ•ด์„œ ๋ฐฉํ–ฅ์„ ๊ฐ™์ด ์ €์žฅํ–ˆ๋‹ค. false๋Š” ์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ, true๋Š” ์˜ค๋ฅธ์ชฝ์—์„œ ์™ผ์ชฝ์œผ๋กœ ์ด๋™ํ•˜๋Š” ๊ฐœ๋ฏธ๋ฅผ ๋œปํ•œ๋‹ค.

์ฒ˜์Œ์—” ์ด์ค‘ for๋ฌธ์„ ์ด์šฉํ–ˆ๋Š”๋ฐ, ๊ฐœ๋ฏธ๋“ค์„ ์Šค์™‘ํ•˜๋Š” ๊ฒฝ์šฐ i๋ฅผ ํ•œ ๋ฒˆ ๋” ๊ฑด๋„ˆ๋›ฐ์–ด์•ผ ํ•ด์„œ while๋ฌธ์œผ๋กœ ๋ณ€๊ฒฝํ–ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์„œ๋กœ ๋ฐฉํ–ฅ์ด ๋‹ค๋ฅธ ๊ฒฝ์šฐ์— ์ „๋ถ€ ์Šค์™‘ํ•ด์คฌ์—ˆ๋Š”๋ฐ, 7% ์ฏค์—์„œ ํ‹€๋ ค์„œ ๋‹ค๋ฅธ ํ’€์ด๋ฅผ ์ฐพ์•„๋ดค๋‹ค. ์™ผ์ชฝ์œผ๋กœ ์ด๋™ํ•˜๊ณ  ์žˆ๋Š” ๊ฐœ๋ฏธ๋ž‘ ๋งŒ๋‚œ ๊ฒฝ์šฐ์—๋Š” ๋ฐ”๊ฟ”์ค„ ํ•„์š”๊ฐ€ ์—†๋‹ค๋Š” ๊ฑฐ์˜€๋‹ค. (←, →) ์„œ๋กœ ๋งŒ๋‚˜์ง€ ์•Š์œผ๋‹ˆ๊นŒ! ๊ทธ๋ž˜์„œ ์•ž์— ์žˆ๋Š” ๊ฐœ๋ฏธ๊ฐ€ ์˜ค๋ฅธ์ชฝ์„ ๋ณด๊ณ  ์žˆ์„ ๋•Œ ์กฐ๊ฑด๋„ ์ถ”๊ฐ€ํ–ˆ๋‹ค.


๋ฌธ์ œ

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


๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ

Swift

let n = readLine()!.split(separator: " ").map { Int(String($0))! }
let a = Array(readLine()!.reversed())
let b = Array(readLine()!)
let t = Int(readLine()!)!
var c = a.map { return ($0, false) } + b.map { return ($0, true) }
let m = n.reduce(0, +)

for _ in 0..<t {
    var i = 1
    while i < m {
        if c[i].1 != c[i-1].1 && c[i - 1].1 == false { // ๋ฐฉํ–ฅ์ด ๋‹ค๋ฅธ ๊ฒฝ์šฐ
            c.swapAt(i, i-1)
            i += 1
        }
        i += 1
    }
}

print(c.map { String($0.0) }.joined())
๋ฐ˜์‘ํ˜•