https://www.acmicpc.net/problem/12891
๋ถ๋ถ๋ฌธ์์ด์ ๊ตฌํ๋ ๋ฌธ์ ๋ก ์ฒ์์๋ for๋ฌธ์ ์ค์ฒฉํด์ ๊ตฌํ๋ ๋ฐฉ๋ฒ์ ์๊ฐํ๋ค. ํ์ง๋ง ์๊ฐ์ด๊ณผ๊ฐ ๋ฌ๋ค. ์ ๋ ฅ๋๋ ๋ฌธ์์ด์ ๊ธธ์ด๊ฐ ์ต๋ 1,000,000(๋ฐฑ๋ง)์ด๋ค. for๋ฌธ์ ์ค์ฒฉํด์ ๋๋ฆฐ๋ค๋ฉด O(n^2) ์ฝ 1,000,000(๋ฐฑ๋ง) * 1,000,000(๋ฐฑ๋ง) = ์ต ๋จ์๊ฐ ๋์ด๊ฐ์ 1์ด ์์ ํ์ง ๋ชปํ๋ค...
์คํจ
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
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());
char[] S = br.readLine().toCharArray();
st = new StringTokenizer(br.readLine(), " ");
int a = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
int g = Integer.parseInt(st.nextToken());
int t = Integer.parseInt(st.nextToken());
//๊ตฌํ
int answer = 0;
for(int i = 0; i <= s - p; i++) {
int a_count = 0, c_count = 0, g_count = 0, t_count = 0;
for(int j = 0; j < p; j++) {
switch(S[i + j]) {
case 'A':
a_count++;
break;
case 'C':
c_count++;
break;
case 'G':
g_count++;
break;
case 'T':
t_count++;
break;
}
}
if(a_count >= a && c_count >= c && g_count >= g && t_count >= t) {
answer++;
}
}
//์ถ๋ ฅ
System.out.println(answer);
}
}
์ด์ค for๋ฌธ์ ์ฐ๋ O(n^2)์ ์๊ฐ์ O(n)์ผ๋ก ์ค์ผ ์ ์๋ ๋ฐฉ๋ฒ์ ์ฌ๋ผ์ด๋ฉ ์๋์ฐ์ด๋ค.
2 ~ i+1๊น์ง์ ํฉ = 1~i๊น์ง์ ํฉ + (i + 1)๋ฒ์งธ ์์ - 1๋ฒ์งธ ์์ .. ์ด๋ฐ์์ผ๋ก ๊ฐฑ์ ํด๋๊ฐ๋ฉด ๋๋ค.
๋ก์ง์ ์ด๊ฑธ ์ฐธ๊ณ ๋ฅผ ํ๋ค.
https://hanyeop.tistory.com/356
์ฝ๋
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
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());
char[] S = br.readLine().toCharArray();
st = new StringTokenizer(br.readLine(), " ");
int a = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
int g = Integer.parseInt(st.nextToken());
int t = Integer.parseInt(st.nextToken());
//๊ตฌํ
//๊ฐ ๊ตฌ๊ฐ ๋ณ๋ก ์ฒดํฌ
int answer = 0;
int a_count = 0, c_count = 0, g_count = 0, t_count = 0;
for(int i = 0; i < s; i++) {
switch(S[i]) { //์๋ก์ด ๋ฌธ์ ์กฐ๊ฑด ๊ฐฑ์
case 'A':
a_count++;
break;
case 'C':
c_count++;
break;
case 'G':
g_count++;
break;
case 'T':
t_count++;
break;
}
if(i == p - 1) { //๊ฐ์ฅ ์ฒ์ ์ํธ๋ฌธ ํ๋จ
if(a_count >= a && c_count >= c && g_count >= g && t_count >= t) {
answer++;
}
}
if(i >= p) { //์ด์ ๋ฌธ์ ์กฐ๊ฑด ๊ฐฑ์
switch(S[i - p]) {
case 'A':
a_count--;
break;
case 'C':
c_count--;
break;
case 'G':
g_count--;
break;
case 'T':
t_count--;
break;
}
if(a_count >= a && c_count >= c && g_count >= g && t_count >= t) {
answer++;
}
}
}
//์ถ๋ ฅ
System.out.println(answer);
}
}
๊ฒฐ๊ณผ
'Algorithm > ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฐฑ์ค 2630๋ฒ - ์์ข ์ด ๋ง๋ค๊ธฐ (0) | 2022.08.07 |
---|---|
๋ฐฑ์ค 11660๋ฒ - ๊ตฌ๊ฐ ํฉ ๊ตฌํ๊ธฐ 5 (0) | 2022.08.06 |
๋ฐฑ์ค 1931๋ฒ - ํ์์ค ๋ฐฐ์ (0) | 2022.08.05 |
๋ฐฑ์ค 2563๋ฒ - ์์ข ์ด (0) | 2022.08.05 |
๋ฐฑ์ค 1916๋ฒ - ์ต์๋น์ฉ ๊ตฌํ๊ธฐ (0) | 2022.04.08 |