ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป ๋ฌธ์ œ

 

1969๋ฒˆ: DNA

DNA๋ž€ ์–ด๋–ค ์œ ์ „๋ฌผ์งˆ์„ ๊ตฌ์„ฑํ•˜๋Š” ๋ถ„์ž์ด๋‹ค. ์ด DNA๋Š” ์„œ๋กœ ๋‹ค๋ฅธ 4๊ฐ€์ง€์˜ ๋‰ดํด๋ ˆ์˜คํ‹ฐ๋“œ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค(Adenine, Thymine, Guanine, Cytosine). ์šฐ๋ฆฌ๋Š” ์–ด๋–ค DNA์˜ ๋ฌผ์งˆ์„ ํ‘œํ˜„ํ•  ๋•Œ, ์ด DNA๋ฅผ ์ด๋ฃจ๋Š” ๋‰ดํด๋ ˆ์˜ค

www.acmicpc.net


โœ๐Ÿป ํ’€์ด

๐ŸŽจ Go

// https://www.acmicpc.net/problem/1969
package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strings"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var n, m int
	fmt.Fscanln(reader, &n, &m)

	var counts = make([]map[string]int, m)
	for i := 0; i < m; i++ {
		counts[i] = map[string]int{} // DNA์˜ ๊ฐ ์œ„์น˜์— ์กด์žฌํ•˜๋Š” ๋‰ดํด๋ ˆ์˜คํ‹ฐ๋“œ ์ข…๋ฅ˜๋ณ„ ๊ฐœ์ˆ˜๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ๋งต ์ƒ์„ฑ
	}
	for i := 0; i < n; i++ {
		var dna string
		fmt.Fscanln(reader, &dna)
		nucleotide := strings.Split(dna, "")
		for j := 0; j < len(nucleotide); j++ {
			counts[j][nucleotide[j]]++
		}
	}

	var answerDNA string
	var answerCount int
	for i := 0; i < len(counts); i++ {
		count := counts[i]
		var pairs []pair
		for k, v := range count {
			pairs = append(pairs, pair{k, v})
		}
		sort.Slice(pairs, func(i, j int) bool {
			if pairs[i].value > pairs[j].value { // ๊ฐ ์œ„์น˜๋ณ„๋กœ ๊ฐ€์žฅ ๋งŽ์ด ์กด์žฌํ•˜๋Š” ๋‰ดํด๋ ˆ์˜คํ‹ฐ๋“œ๋ฅผ ์ฑ„ํƒ
				return true
			} else if pairs[i].value == pairs[j].value {
				return pairs[i].key < pairs[j].key // ๋™์ผํ•œ ๊ฐœ์ˆ˜์ผ ๊ฒฝ์šฐ ์‚ฌ์ „์ˆœ์œผ๋กœ ์ฑ„ํƒ
			}
			return false
		})
		answerDNA += pairs[0].key         // ์ฑ„ํƒ๋œ ๋‰ดํด๋ ˆ์˜คํ‹ฐ๋“œ ๊ฐ’ ์ถ”๊ฐ€
		answerCount += n - pairs[0].value // DNA ๊ฐœ์ˆ˜์—์„œ ์ฑ„ํƒ๋œ ๋‰ดํด๋ ˆ์˜คํ‹ฐ๋“œ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋บ€ ๊ฐ’์„ hamming distance๋กœ ๊ณ„์‚ฐ
	}

	fmt.Fprintln(writer, answerDNA)
	fmt.Fprintln(writer, answerCount)
}

type pair struct {
	key   string
	value int
}

๐ŸŽจ Python3

# https://www.acmicpc.net/problem/1969
import sys

if __name__ == "__main__":
    n, m = list(map(int, sys.stdin.readline().split()))
    counts = [{} for i in range(m)]
    
    for i in range(n):
        dna = sys.stdin.readline()
        nucleotide = list(dna)
        for j in range(m):
            if counts[j].get(nucleotide[j]):
                counts[j][nucleotide[j]] += 1
            else:
                counts[j][nucleotide[j]] = 1
    
    answer_dna = ""
    answer_count = 0
    for i in range(len(counts)):
        count = counts[i]
        answer = sorted(count.items(), key=lambda item: (-item[1], item[0]))[0]
        answer_dna += answer[0]
        answer_count += n - answer[1]
        
    print("{}\n{}".format(answer_dna, answer_count))
728x90
๋Œ“๊ธ€