Home (Python)(백준_12891) DNA 비밀번호
Post
Cancel

(Python)(백준_12891) DNA 비밀번호

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
'''
https://www.acmicpc.net/problem/12891

특정 배열에서 크기가 고정인 부분 배열을 빠르게 처리해야하기 때문에 sliding window 사용(sliding window의 시간복잡도 = O(n))
'''

import sys

input = sys.stdin.readline

S, P = map(int, input().split()) # 4 2
input_string = list(input().strip()) # GATA
A, C, G, T = map(int, input().split()) # 1 0 0 1

substring = input_string[:P-1]
dic = {'A': 0, 'C': 0, 'G': 0, 'T': 0}
cnt = 0

for i in substring:
    dic[i] += 1

# 부분 문자열 슬라이딩 윈도우 이용하여 전체 문자열 확인
for i in range(P-1, S): # i = 1 2 3
    dic[input_string[i]] += 1  # 윈도우 오른쪽 끝 문자 추가

    # 현재 윈도우가 조건을 만족하는지 확인
    if dic['A'] >= A and dic['C'] >= C and dic['G'] >= G and dic['T'] >= T:
        cnt += 1

    dic[input_string[i-P+1]] -= 1  # 윈도우 왼쪽 끝 문자 제거

print(cnt)
This post is licensed under CC BY 4.0 by the author.