본문 바로가기
알고리즘/구현(백준)

[2023-06-06] 1969 DNA (Kotlin)

by joh9911 2023. 6. 6.

 

문제 링크: https://www.acmicpc.net/problem/1969

 

1969번: DNA

DNA란 어떤 유전물질을 구성하는 분자이다. 이 DNA는 서로 다른 4가지의 뉴클레오티드로 이루어져 있다(Adenine, Thymine, Guanine, Cytosine). 우리는 어떤 DNA의 물질을 표현할 때, 이 DNA를 이루는 뉴클레오

www.acmicpc.net

 

문제를 잘못 읽어 30분이나 걸렸습니다.

 

package sixtythird
import java.io.*
fun main(){
    val br = BufferedReader(InputStreamReader(System.`in`))
    val (n,m) = br.readLine().split(' ').map{it.toInt()}
    // 각 자리수 등장 횟수 더하기
    val arr = Array(m){Array(4){0} }
    repeat(n){
        val input = br.readLine()
        for (index in input.indices){
            when(input[index]){
                'A' -> arr[index][0] += 1
                'C' -> arr[index][1] += 1
                'G' -> arr[index][2] += 1
                'T' -> arr[index][3] += 1
            }
        }
    }
    
    var count = 0
    for (i in arr.indices){
        val max = arr[i].max()
        // 사전별 출력이므로, 출현 횟수가 최대인 것을 찾으면 break
        for (j in arr[i].indices){
            if (arr[i][j] == max){
                when(j){
                    0 -> print("A")
                    1 -> print("C")
                    2 -> print("G")
                    3 -> print("T")
                }
                break
            }
        }
        // 등장 횟수의 sum 에서 max 값 빼주기
        count += arr[i].sum() - max
    }
    println()
    println(count)

}

 

댓글