Skip to content

Commit 47a7df6

Browse files
committed
[String] baekjoon-6503
1 parent d2b9b10 commit 47a7df6

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

Diff for: β€ŽREADME.md

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
| 10 | | [Baekjoon-5052 μ „ν™”λ²ˆν˜Έ λͺ©λ‘](./src/String/P5052) | |
103103
| 11 | | [Baekjoon-17413 단어 λ’€μ§‘κΈ° 2](./src/String/P17413) | |
104104
| 12 | | [Baekjoon-9342 염색체](./src/String/P9342) | |
105+
| 13 | | [Baekjoon-6503 망가진 ν‚€λ³΄λ“œ](./src/String/P6503) | 2-pointer |
105106

106107
## Brute Force
107108

Diff for: β€Žsrc/String/P6503/Main.java

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package String.P6503;
2+
3+
import java.io.*;
4+
5+
public class Main {
6+
7+
public static void main(String[] args) throws Exception {
8+
System.setIn(new FileInputStream("src/String/P6503/input.txt"));
9+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
StringBuilder sb = new StringBuilder();
11+
12+
int m;
13+
while ((m = Integer.parseInt(br.readLine())) != 0) {
14+
String s = br.readLine();
15+
int[] cnt = new int[128];
16+
17+
int low = 0, high = 0, size = 1, max = 0;
18+
cnt[s.charAt(0)] = 1;
19+
while (low <= high) {
20+
if (size <= m && (high+1) < s.length()) {
21+
max = Math.max(max, (high - low + 1));
22+
high ++;
23+
int h = s.charAt(high);
24+
cnt[h] ++;
25+
if (cnt[h] == 1) size ++;
26+
} else {
27+
if (size <= m) max = Math.max(max, (high - low + 1));
28+
char l = s.charAt(low);
29+
cnt[l] --;
30+
if (cnt[l] == 0) size --;
31+
low ++;
32+
}
33+
}
34+
sb.append(max).append("\n");
35+
}
36+
37+
System.out.print(sb);
38+
}
39+
}

Diff for: β€Žsrc/String/P6503/README.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
## [baekjoon-6503] 망가진 ν‚€λ³΄λ“œ
2+
3+
![image](https://user-images.githubusercontent.com/22045163/123613388-5759b180-d83e-11eb-89c4-c8a6bf347333.png)
4+
5+
### 풀이 κ³Όμ •
6+
7+
μ„œλ‘œ λ‹€λ₯Έ 문자의 개수λ₯Ό κ΅¬ν•˜λŠ” κ³Όμ •μ—μ„œ μ²˜μŒμ— ν•΄μ‹œλ§΅μ„ μ‚¬μš©ν–ˆλŠ”λ°, μ‹€ν–‰ μ‹œκ°„μ΄ λ„ˆλ¬΄ λ†’κ²Œ λ‚˜μ™€μ„œ μ•Œκ³  λ³΄λ‹ˆ `m` λ²”μœ„λ₯Ό `128`κΉŒμ§€ μ€€ μ΄μœ κ°€ μžˆμ—ˆλ‹€. μ•„μŠ€ν‚€μ½”λ“œκ°€ `0~127` λ²”μœ„λΌλŠ” 것. κ·Έλž˜μ„œ `128` μ‚¬μ΄μ¦ˆμ˜ 배열을 μ„ μ–Έν•΄μ„œ μΉ΄μš΄νŠΈν•˜λŠ” μ‹μœΌλ‘œ λ°”κΏ”μ€¬λ”λ‹ˆ μ‹€ν–‰ μ‹œκ°„μ΄ κ°œμ„ λ˜μ—ˆλ‹€.
8+
9+
![image](https://user-images.githubusercontent.com/22045163/123613441-62144680-d83e-11eb-8aeb-a4bef1fa2191.png)

Diff for: β€Žsrc/String/P6503/input.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
5
2+
This can't be solved by brute force.
3+
1
4+
Mississippi
5+
0

0 commit comments

Comments
Β (0)