-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain.java
39 lines (33 loc) Β· 1.21 KB
/
Main.java
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
33
34
35
36
37
38
39
package String.P6503;
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("src/String/P6503/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int m;
while ((m = Integer.parseInt(br.readLine())) != 0) {
String s = br.readLine();
int[] cnt = new int[128];
int low = 0, high = 0, size = 1, max = 0;
cnt[s.charAt(0)] = 1;
while (low <= high) {
if (size <= m && (high+1) < s.length()) {
max = Math.max(max, (high - low + 1));
high ++;
int h = s.charAt(high);
cnt[h] ++;
if (cnt[h] == 1) size ++;
} else {
if (size <= m) max = Math.max(max, (high - low + 1));
char l = s.charAt(low);
cnt[l] --;
if (cnt[l] == 0) size --;
low ++;
}
}
sb.append(max).append("\n");
}
System.out.print(sb);
}
}