728x90
https://www.acmicpc.net/problem/12891
package BaekJoon.SlidingWindow;
import java.util.*;
import java.io.*;
public class DNA비밀번호 {
static int p,s;
static int answer=0;
static int a,c,g,t;
public static boolean check(HashMap<Character,Integer>hm) {
Set<Character> key = hm.keySet();
for(Iterator it =key.iterator(); it.hasNext();) {
Character keyValue = (Character)it.next();
if(keyValue=='A' && hm.get(keyValue)<a) {return false;}
else if(keyValue=='C' && hm.get(keyValue)<c) {return false;}
else if(keyValue=='G' && hm.get(keyValue)<g) {return false;}
else if(keyValue=='T' && hm.get(keyValue)<t) {return false;}
}
return true;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
p =Integer.parseInt(st.nextToken());
s = Integer.parseInt(st.nextToken());
HashMap<Character,Integer>hm = new HashMap<>();
String str = br.readLine();
st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
a = n;
hm.put('A', 0);
n = Integer.parseInt(st.nextToken());
c = n;
hm.put('C', 0);
n = Integer.parseInt(st.nextToken());
g = n;
hm.put('G', 0);
n = Integer.parseInt(st.nextToken());
t = n;
hm.put('T', 0);
for(int i=0; i<s; i++) {
char cc = str.charAt(i);
hm.put(cc, hm.get(cc)+1);
}
if(check(hm))answer++;
for(int i=1; i+s-1<p; i++) {
char prevc = str.charAt(i-1);
char nextc = str.charAt(i+s-1);
hm.put(prevc, hm.get(prevc)-1);
hm.put(nextc, hm.get(nextc)+1);
if(check(hm))answer++;
}
System.out.println(answer);
}
}
'백준 > 슬라이딩윈도우' 카테고리의 다른 글
회전초밥 - Java (0) | 2021.10.06 |
---|---|
N번째 큰수 (0) | 2021.10.02 |
가장긴짝수연속합부분수열 (0) | 2021.10.02 |
게으른 백곰 - 10025번(Java) (0) | 2021.09.16 |
꿀아르바이트 - 12847번(Java) (0) | 2021.09.16 |