Skip to content

Commit 8ebe159

Browse files
committed
[Gold IV] Title: 고냥이, Time: 196 ms, Memory: 19388 KB -BaekjoonHub
1 parent 9087121 commit 8ebe159

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# [Gold IV] 고냥이 - 16472
2+
3+
[문제 링크](https://www.acmicpc.net/problem/16472)
4+
5+
### 성능 요약
6+
7+
메모리: 19388 KB, 시간: 196 ms
8+
9+
### 분류
10+
11+
두 포인터
12+
13+
### 제출 일자
14+
15+
2025년 4월 22일 10:44:38
16+
17+
### 문제 설명
18+
19+
<p>고양이는 너무 귀엽다. 사람들은 고양이를 너무 귀여워했고, 결국 고양이와 더욱 가까워지고 싶어 고양이와의 소통을 위한 고양이 말 번역기를 발명하기로 했다. 이 번역기는 사람의 언어를 고양이의 언어로, 고양이의 언어를 사람의 언어로 바꾸어 주는 희대의 발명품이 될 것이다.</p>
20+
21+
<p>현재 고양이말 번역기의 베타버전이 나왔다. 그러나 이 베타버전은 완전 엉망진창이다. 베타버전의 번역기는 문자열을 주면 그 중에서 최대 N개의 종류의 알파벳을 가진 연속된 문자열밖에 인식하지 못한다. 굉장히 별로지만 그나마 이게 최선이라고 사람들은 생각했다. 그리고 문자열이 주어졌을 때 이 번역기가 인식할 수 있는 최대 문자열의 길이는 얼마인지가 궁금해졌다.</p>
22+
23+
<p>고양이와 소통할 수 있도록 우리도 함께 노력해보자.</p>
24+
25+
### 입력
26+
27+
<p>첫째 줄에는 인식할 수 있는 알파벳의 종류의 최대 개수 N이 입력된다. (1 < N ≤ 26)</p>
28+
29+
<p>둘째 줄에는 문자열이 주어진다. (1 ≤ 문자열의 길이 ≤ 100,000) 단, 문자열에는 알파벳 소문자만이 포함된다.</p>
30+
31+
### 출력
32+
33+
<p>첫째 줄에 번역기가 인식할 수 있는 문자열의 최대길이를 출력한다. </p>
34+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
static int n, ans;
6+
static char s[];
7+
static StringTokenizer st;
8+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
9+
public static void main(String[] args) throws Exception{
10+
inputSetting();
11+
sizeCheck();
12+
System.out.println(ans);
13+
}
14+
15+
static void sizeCheck(){
16+
Map<Character, Integer> alpha = new HashMap<>();
17+
int l, r, type;
18+
19+
l = 0;
20+
r = 0;
21+
type = 0;
22+
23+
while(r < s.length){
24+
if(alpha.getOrDefault(s[r], 0) == 0){
25+
type++;
26+
alpha.put(s[r], 1);
27+
28+
while(n < type && l <= r) {
29+
if (alpha.get(s[l]) == 1){
30+
type--;
31+
alpha.remove(s[l]);
32+
}
33+
else alpha.put(s[l], alpha.get(s[l]) - 1);
34+
35+
l++;
36+
}
37+
}
38+
else alpha.put(s[r], alpha.get(s[r]) + 1);
39+
40+
ans = Math.max(ans, r - l + 1);
41+
r++;
42+
}
43+
}
44+
45+
static void inputSetting() throws Exception{
46+
ans = 1;
47+
n = Integer.parseInt(br.readLine());
48+
s = br.readLine().toCharArray();
49+
}
50+
}

0 commit comments

Comments
 (0)