https://www.acmicpc.net/problem/12891
import java.io.*;
import java.util.*;
public class Main {
static int S;
static int P;
static char[] dna;
static int[] rule;
static int[] now;
static boolean check(int[] rule, int[] now) {
for (int i = 0; i < 4; i++) {
if (rule[i] > now[i]) return false; // 규칙 숫자에 미치지 못할 경우 실패!
}
return true;
}
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(bf.readLine());
S = Integer.parseInt(st.nextToken());
P = Integer.parseInt(st.nextToken());
dna = bf.readLine().toCharArray();
rule = new int[4];
st = new StringTokenizer(bf.readLine());
for (int i = 0; i < 4; i++) {
rule[i] = Integer.parseInt(st.nextToken());
}
now = new int[4];
int ans = 0;
// 우선 맨 처음 ~ P까지 확인
for (int i = 0; i < P; i++) {
if (dna[i] == 'A') now[0] += 1;
else if (dna[i] == 'C') now[1] += 1;
else if (dna[i] == 'G') now[2] += 1;
else if (dna[i] == 'T') now[3] += 1;
}
if (check(rule, now)) ans += 1;
// 탐색 i를 끝점으로 잡아서 탐색!
for (int i = P; i < S; i++) {
int start = i - P; // 탐색하며 빠지게 됨
if (dna[start] == 'A') now[0] -= 1;
else if (dna[start] == 'C') now[1] -= 1;
else if (dna[start] == 'G') now[2] -= 1;
else if (dna[start] == 'T') now[3] -= 1;
if (dna[i] == 'A') now[0] += 1;
else if (dna[i] == 'C') now[1] += 1;
else if (dna[i] == 'G') now[2] += 1;
else if (dna[i] == 'T') now[3] += 1;
if (check(rule, now)) ans += 1;
}
System.out.println(ans);
}
}
dna = bf.readLine().toCharArray();
public class Main {
static int S;
static int P;
static String str;
static int[] rule;
static int[] now;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
S = Integer.parseInt(st.nextToken());
P = Integer.parseInt(st.nextToken());
str = br.readLine();
rule = new int[4];
now = new int[4];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < 4; i++) {
rule[i] = Integer.parseInt(st.nextToken());
}
int ans = 0;
for (int i = 0; i < P; i++) {
updateCount(str.charAt(i), 1);
}
if (check()) {
ans++;
}
for (int i = P; i < S; i++) {
updateCount(str.charAt(i - P), -1);
updateCount(str.charAt(i), 1);
if (check()) {
ans++;
}
}
System.out.println(ans);
}
static void updateCount(char ch, int delta) {
if (ch == 'A') {
now[0] += delta;
} else if (ch == 'C') {
now[1] += delta;
} else if (ch == 'G') {
now[2] += delta;
} else if (ch == 'T') {
now[3] += delta;
}
}
static boolean check() {
for (int i = 0; i < 4; i++) {
if (now[i] < rule[i]) {
return false;
}
}
return true;
}
}