๊ธฐ๋ก๋ฐฉ

BOJ_12891 : DNA ๋น„๋ฐ€๋ฒˆํ˜ธ ๋ณธ๋ฌธ

CodingTest/Java

BOJ_12891 : DNA ๋น„๋ฐ€๋ฒˆํ˜ธ

Soom_1n 2022. 11. 3. 17:37

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

12891๋ฒˆ: DNA ๋น„๋ฐ€๋ฒˆํ˜ธ

ํ‰์†Œ์— ๋ฌธ์ž์—ด์„ ๊ฐ€์ง€๊ณ  ๋…ธ๋Š” ๊ฒƒ์„ ์ข‹์•„ํ•˜๋Š” ๋ฏผํ˜ธ๋Š” DNA ๋ฌธ์ž์—ด์„ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค. DNA ๋ฌธ์ž์—ด์€ ๋ชจ๋“  ๋ฌธ์ž์—ด์— ๋“ฑ์žฅํ•˜๋Š” ๋ฌธ์ž๊ฐ€ {‘A’, ‘C’, ‘G’, ‘T’} ์ธ ๋ฌธ์ž์—ด์„ ๋งํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด “ACKA”

www.acmicpc.net



๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • ์ž…๋ ฅ๋œ ๋ฌธ์ž์—ด S์—์„œ P ๊ธธ์ด์˜ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ๋งŒ๋“ค์—ˆ์„๋•Œ, ์ž…๋ ฅ๋œ DNA ๋ฌธ์ž์—ด ์ตœ์†Œ ๊ฐœ์ˆ˜๋ฅผ ์ถฉ์กฑํ•˜๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
  • ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด์šฉํ•ด์„œ P๊ธธ์ด์˜ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ๋ฌธ์ž์—ด S์˜ ์‹œ์ž‘์ ๋ถ€ํ„ฐ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ๋ฐ€์–ด๊ฐ„๋‹ค.
    • ์œˆ๋„์šฐ์—์„œ ๋ฒ—์–ด๋‚˜๋Š” ์ธ๋ฑ์Šค๋Š” ํ˜„์žฌ ์ƒํƒœ์—์„œ ๋นผ๊ธฐ
    • ์œˆ๋„์šฐ์— ์ƒˆ๋กœ ๋“ค์–ด์˜ค๋Š” ์ธ๋ฑ์Šค๋Š” ํ˜„์žฌ ์ƒํ…Œ์— ๋”ํ•˜๊ธฐ
    • ์กฐ๊ฑด์— ์ถฉ์กฑ ํ•˜๋Š”์ง€ ํ™•์ธํ•ด์„œ ์นด์šดํŠธ

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
    static int arr[], now[];

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int s = Integer.parseInt(st.nextToken());
        int p = Integer.parseInt(st.nextToken());
        String dna = br.readLine();
        arr = new int[4];
        now = new int[4];

        // ์ฒดํฌ ๋ฐฐ์—ด ์ž…๋ ฅ ๋ฐ›๊ธฐ
        st= new StringTokenizer(br.readLine());
        for (int i = 0; i < 4; i++)
            arr[i] = Integer.parseInt(st.nextToken());

        // ํ˜„์žฌ ์ƒํƒœ ๋ฐฐ์—ด๊ณผ ์ดˆ๊ธฐํ™”
        for (int i = 0; i < p; i++){ now[idx(dna.charAt(i))]++; }

        int answer = check() ? 1:0;

        // ์œˆ๋„์šฐ ์ด๋™
        for (int i = 1; i <= s - p; i++){
            now[idx(dna.charAt(i-1))]--;
            now[idx(dna.charAt(i+p-1))]++;
            if (check())
                answer++;
        }
        System.out.println(answer);
    }

    // ๋ฌธ์ž์˜ DNA ์ธ๋ฑ์Šค ์œ„์น˜
    private static int idx(char c){
        if (c == 'A')
            return 0;
        else if (c == 'C') {
            return 1;
        }
        else if (c == 'G') {
            return 2;
        }
        else if (c == 'T') {
            return 3;
        }
        return 0;
    }

    // ์ฒดํฌ ๋ฐฐ์—ด๊ณผ ํ˜„์žฌ ์ƒํƒœ ๋ฐฐ์—ด ๋น„๊ต
    private static boolean check(){
        boolean flag = true;
        for (int i = 0; i < 4; i++){
            if (now[i] < arr[i]){
                flag = false;
                break;
            }
        }
        return flag;
    }
}

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • ๋ฉ”์†Œ๋“œ์—์„œ ๊ณต์šฉ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์ƒํƒœ ๋ฐฐ์—ด arr์™€ ํ˜„์žฌ ์ƒํƒœ ๋ฐฐ์—ด now๋ฅผ ์ •์  ๋ณ€์ˆ˜๋กœ ์„ ์–ธํ•ด๋‘”๋‹ค.
  • ๋ฉ”์†Œ๋“œ idx()์—์„œ ๋ฌธ์ž๊ฐ€ ๋ฐฐ์—ดarr์˜ ๋ช‡ ๋ฒˆ์žฌ ์ธ๋ฑ์Šค์ธ์ง€ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • ๋ฉ”์†Œ๋“œ check() ์—์„œ ๋ฐฐ์—ด arr์™€ now๊ฐ€ ๊ฐ™์€์ง€ ๋น„๊ตํ•ด์„œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • main() ๋ฉ”์†Œ๋“œ์—์„œ ๋ฌธ์ œ ์กฐ๊ฑด์˜ ์ž…๋ ฅ์„ ๋ฐ›๊ณ , ์œˆ๋„์šฐ๋ฅผ ์ด๋™ํ•˜๋ฉฐ ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•œ ๊ฒฝ์šฐ๋ฅผ ์นด์šดํŠธํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ๊ต์žฌ ๊ณต๋ถ€๋กœ ์ ‘ํ•œ ๋ฌธ์ œ์ธ๋ฐ, ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์œ ์šฉํ•œ ๊ฒƒ ๊ฐ™๋‹ค.

728x90

'CodingTest > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

BOJ_1874 : ์Šคํƒ ์ˆ˜์—ด  (0) 2022.11.07
BOJ_11003 : ์ตœ์†Ÿ๊ฐ’ ์ฐพ๊ธฐ  (0) 2022.11.03
BOJ_1120 : ๋ฌธ์ž์—ด  (0) 2022.11.02
BOJ_1064 : ํ‰ํ–‰์‚ฌ๋ณ€ํ˜•  (0) 2022.11.01
BOJ_1059 : ์ข‹์€ ๊ตฌ๊ฐ„  (0) 2022.10.30