ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ฉ๐ป๐ป ๋ฌธ์
โ๐ป ํ์ด
๐จ 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
'dev > algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ / 2422๋ฒ / ํ์ค์ ์ด ์ดํ๋ฆฌ์์ ๊ฐ์ ์์ด์คํฌ๋ฆผ์ ์ฌ๋จน๋๋ฐ [Go][Python3] (0) | 2021.01.26 |
---|---|
BOJ / 1145๋ฒ / ์ ์ด๋ ๋๋ถ๋ถ์ ๋ฐฐ์ [Go][Python3] (0) | 2021.01.25 |
BOJ / 2503๋ฒ / ์ซ์ ์ผ๊ตฌ [Go][Python3] (0) | 2021.01.23 |
BOJ / 17626๋ฒ / Four Squares [Go][Python3] (0) | 2021.01.21 |
BOJ / 19947๋ฒ / ํฌ์์ ๊ท์ฌ ๋ฐฐ์ฃผํ [Go][Python3] (0) | 2021.01.20 |
๋๊ธ
๊ธ ๋ณด๊ดํจ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ
TAG
- ๋ฐฑ์ค
- ballet
- ํ
- ์คํ
- dfs
- dp
- java
- MongoDB
- ํ๋ก์ด๋์์ฌ
- ์๊ฐ๊ต์ฒด
- ์๊ณ ๋ฆฌ์ฆ
- python3
- Golang
- AWS
- BOJ
- ๋งฅ๋ถ
- Algorithm
- ์ด๋ถํ์
- Macbook pro 2012 mid 13
- ๋งฅ๋ถ ์ ๊ทธ๋ ์ด๋
- go
- ํด์๋งต
- ์๋ฐ
- ๋ชฝ๊ณ ๋๋น
- ๋งฅ๋ถํ๋ก
- baekjoon
- ๋ฐ๋
- BFS
- ๋ถํ ์ ๋ณต
- ๋ธ๋ฃจํธํฌ์ค
- Total
- Today
- Yesterday