๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ฏ | ๋ฐฑ์ค€/๐Ÿ˜€ | Bronze

[Kotlin, B1] ๋ฐฑ์ค€ 20632๋ฒˆ ์œ ๋‹ˆ๋Œ€์ „ ํ€ด์ฆˆ์‡ผ

by immgga 2024. 8. 28.

์ถœ์ฒ˜: unsplash.com

 

์œ ๋‹ˆ๋Œ€์ „ ํ€ด์ฆˆ์‡ผ(20632๋ฒˆ)

Bronze 1

#๊ตฌํ˜„ #๋ฌธ์ž์—ด

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

 

๋ฌธ์ œ ๋‚ด์šฉ

 

 

๋ฌธ์ œ ์ ‘๊ทผ

์ •๋‹ต์ž์˜ ์ฑ„ํŒ…์ด ์˜ฌ๋ผ์˜ค๊ณ  ๊ทธ ์ด์ „์— ์ •๋‹ต์ž์™€ ๊ฐ™์€ ์ฑ„ํŒ…์„ ์นœ ์‚ฌ๋žŒ๋“ค์ด ๋ช‡ ๋ช…์ธ์ง€ ๊ตฌํ•ด์•ผ ํ•œ๋‹ค.

์ž…๋ ฅ์€ ์‹œ๊ฐ„ ์ˆœ์„œ๋Œ€๋กœ ์ฃผ์–ด์ง€๊ณ  ์ •๋‹ต์ž๊ฐ€ ์ž…๋ ฅ๋˜๊ณ  ๋‚˜์„œ ์ด์ „์— ์ฑ„ํŒ…์„ ์นœ ์‚ฌ๋žŒ๋“ค ์ค‘ ์ •๋‹ต์ž์™€ ๊ฐ™์€ ์ฑ„ํŒ…์„ ์นœ ์‚ฌ๋žŒ์„ ๊ตฌํ•ด์•ผ ํ•œ๋‹ค.

๊ทธ๋Ÿฌ๋ฉด ์ž…๋ ฅ์„ ๋ฐ›์„ ๋•Œ ์ •๋‹ต์ž๊ฐ€ ์ž…๋ ฅ๋˜๋ฉด ์ •๋‹ต์ž๊ฐ€ ๋ช‡ ๋ฒˆ์งธ ์ฑ„ํŒ…์—์„œ ์ž…๋ ฅ๋˜์—ˆ๋Š”์ง€, ์ •๋‹ต์€ ๋ฌด์—‡์ธ์ง€ ํ™•์ธ์„ ํ•ด์ค˜์•ผ ํ•œ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  ์ž…๋ ฅ๋ฐ›์€ ์ฑ„ํŒ…๋“ค๊ณผ ์‚ฌ์šฉ์ž ์ด๋ฆ„์„ ๋ชจ๋‘ List๋กœ 2์ฐจ์› List์— ์ €์žฅํ•ด ์ค€๋‹ค.

2์ฐจ์› List๋ฅผ ๊ฑฐ๊พธ๋กœ ๋Œ๊ฒŒ ๋˜๋ฉด ๋ฐ˜๋ณต์˜ ๋งˆ์ง€๋ง‰์ด ๊ฐ€์žฅ ๋จผ์ € ์ž…๋ ฅ๋œ ๊ฐ’์ด ๋˜๊ณ  ๊ฐ€์žฅ ๋นจ๋ฆฌ ์˜ฌ๋ผ์˜จ ์ฑ„ํŒ…์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ •๋‹ต์ž ์ด์ „์— ์ •๋‹ต์„ ์˜ฌ๋ฆฐ ์‚ฌ์šฉ์ž๊ฐ€ ๋ช‡ ๋ช…์ธ์ง€ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

 

 

๋ฌธ์ œ ํ•ด๊ฒฐ ์ฝ”๋“œ

๋”๋ณด๊ธฐ
import java.io.BufferedReader
import java.io.InputStreamReader

fun main() = with(BufferedReader(InputStreamReader(System.`in`))) {
    val (case, correctPerson) = readLine().split(" ")
    val chattingInfo = mutableListOf<List<String>>()
    var correctIndex = 0
    var correct = ""

    for (i in 0 until case.toInt()) {
        val (person, chatting) = readLine().split(" ")

        if (person == correctPerson) {
            correctIndex = i
            correct = chatting
        }
        chattingInfo.add(listOf(person, chatting))
    }

    var disappoint = 0
    for (i in correctIndex - 1 downTo 0) {
        if (chattingInfo[i].last() == correct) disappoint++
    }

    println(disappoint)
}

 

๋ฌธ์ œ ํ’€์ด

์ฑ„ํŒ…์„ ์ž…๋ ฅ๋œ ์ˆœ์„œ๋Œ€๋กœ chattingInfo์— ์ €์žฅํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ €์žฅํ•˜๋‹ค๊ฐ€ ๋„์ค‘์— ์ •๋‹ต์ž๊ฐ€ ์ž…๋ ฅ๋˜๋ฉด ์ •๋‹ต ์ฑ„ํŒ…(correct)๊ณผ ๋ช‡ ๋ฒˆ์งธ ์ฑ„ํŒ…(correctIndex)์ธ์ง€ ์ €์žฅํ•œ๋‹ค.

๋งˆ์ง€๋ง‰์— ๋ฐ˜๋ณต์„ ์—ญ์ˆœ์œผ๋กœ ์ง„ํ–‰ํ•ด์„œ ์ •๋‹ต์ž ์ด์ „์˜ ์ฑ„ํŒ… ์ค‘ ์ •๋‹ต์ž์™€ ๊ฐ™์€ ์ฑ„ํŒ…์„ ์นœ ์‚ฌ๋žŒ๋งŒ ๊ตฌํ•ด์ค€๋‹ค.

 

 

๋ฌธ์ œ ํ•ด๊ฒฐ ๊ณผ์ •

๋‹จ์ˆœ ๊ตฌํ˜„ ๋ฌธ์ œ.

map์„ ์ด์šฉํ•˜๋ฉด ๋” ์‰ฌ์šธ ๊ฒƒ ๊ฐ™๊ธด ํ–ˆ์ง€๋งŒ map์ด ์ž…๋ ฅ ์ˆœ์œผ๋กœ ์ž˜ ๋“ค์–ด์˜ค๋Š”์ง€ ์•Œ ์ˆ˜ ์—†์–ด์„œ 2์ค‘ List๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.

728x90