๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm๐Ÿฐ/๋ฐฑ์ค€

[๋ฐฑ์ค€] 1969 DNA

by Jouureee 2022. 2. 16.

๋ฌธ์ œ :

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

 

1969๋ฒˆ: DNA

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

www.acmicpc.net

 

ํ’€์ด :

๊ฐ์œผ๋กœ ์—ด๊ฐ„ ๋น„๊ต๋ฅผ ํ†ตํ•ด ๊ฐ€์žฅ ๋งŽ์ด ๋‚˜์˜จ ๋ฌธ์ž๊ฐ€ ๊ทธ ์ž๋ฆฌ ์ธ๋ฑ์Šค๋ผ๋Š” ๊ฒƒ์„ ์•Œ์•˜์ง€๋งŒ ๋ฌธ์ž๊ฐ„์˜ ๊ฑฐ๋ฆฌ์˜ ์ฐจ๋ฅผ ๋ชจ๋‘ ๋”ํ•˜๋Š” ๊ฒƒ์ด ์–ด๋–ป๊ฒŒ ํ•˜๋ฉด ์ข‹์„์ง€ ๋ฐฉ๋ฒ•์ด ์ƒ๊ฐ๋‚˜์ง€ ์•Š์•˜๋‹ค.

 

๊ทธ๋Ÿฌ๋‹ค

https://gusdnr69.tistory.com/31

์š” ๋ธ”๋กœ๊ทธ๋ฅผ ์ฐธ๊ณ ํ•ด ๋ฌธ์ž์™€ ๋‹ฌ๋ž๋˜ ๊ฐ’์„ ์—ด๋กœ ์นด์šดํŠธ ํ–ˆ๋‹ค๋Š” ์ ์ด ์‹ ๊ธฐํ–ˆ๋‹ค. (์•„์ง๋„ ๋ฐฐ์šธ๊ฒŒ ๋„ˆ๋ฌด ๋งŽ๋‹ค๋‹ˆ ๋งŽ์ด ๋ฐ˜์„ฑ ๋œ๋‹ค ..)

์šฐ์„  ๋ฌธ์ž๋ฅผ string ๋ฐฐ์—ด์— ํ•œ๋ฒˆ์— ๋‹ด๊ณ , 

ํ–‰ ์šฐ์„  ๋น„๊ต๊ฐ€ ์•„๋‹Œ ์—ด ์šฐ์„  ๋น„๊ต word[j][i]๋ฅผ ํ†ตํ•ด ๊ฐ€์žฅ ๋งŽ์ด ๋“ฑ์žฅํ•œ ๋ฌธ์ž๋ฅผ unordered_map์œผ๋กœ ๋ฌธ์ž, ๋นˆ๋„์ˆ˜ ๋กœ ์ €์žฅํ•œ๋‹ค.

 

๊ทธ๋ฆฌ๋กœ ๋˜ ํ•œ๋ฒˆ์˜ for๋ฌธ์„ ํ†ตํ•ด max_count์™€ max_index๋ฅผ ์—…๋ฐ์ดํŠธ ์‹œ์ผœ์ค€ ๋’ค ๋น„๊ตํ•œ ์—ด ๊ฐœ์ˆ˜(N)์—์„œ max ๋นˆ๋ฒˆ๋„๋ฅผ ๋นผ์ฃผ์–ด 

๊ตฌํ•˜๊ณ ์ž ํ•œ s์™€ ๋ฌธ์ž์—ด s1, s2, ~ sN ์ค‘ ๋‹ค๋ฅธ ๋ฌธ์ž๋ฅผ ๊ฐ€์ง„ ๋ฌธ์ž์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•œ๋‹ค.

 

c++ ์ฝ”๋“œ :

//
//  1969_DNA.cpp
//  SOMA๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป
//
//  Created by JoSoJeong on 2022/02/16.
//

#include <iostream>
#include <unordered_map>
using namespace std;
int N, cnt;

unordered_map<char, int> map;
int result_sum = 0;

int main(){
    cin >> N >> cnt;
    string s = "";
    string word[N];
    
    for(int i = 0; i < 26; i++){ //initial
        map['A' + i] = 0;
    }
    
    for(int i =0; i < N; i++){
        cin >> word[i];
    }
    
    for(int i =0; i < cnt; i++){ // ํ•œ ์ค„
        for(int j = 0; j < N; j++){
            map[word[j][i]]++;
        }
        
        int max_count = 0; int max_index = 0;
        
        for(int j = 0; j < 26; j++){
            if(map[j+'A'] > max_count) {
                max_count = map[j+'A'];
                max_index = j;
            }
        }

        result_sum += N - max_count;
        s += max_index + 'A';
        
        map.clear();

        
    }
    
    cout << s << '\n';
    cout << result_sum << '\n';
    return 0;
}

๋Œ“๊ธ€