Skip to content

Commit 0074b8c

Browse files
committed
[Combination] baekjoon-1256
1 parent ced8455 commit 0074b8c

File tree

4 files changed

+94
-1
lines changed

4 files changed

+94
-1
lines changed

β€ŽREADME.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,5 @@
7171
| :-: | :----: | :------------------------------------------------------------------------------------------------------------------ | :------------------------ |
7272
| 01 | | [Baekjoon-11050 이항 κ³„μˆ˜ 1](https://github.com/Seogeurim/Algorithm-practice/blob/master/src/Combination/P11050) | |
7373
| 02 | | [Baekjoon-11051 이항 κ³„μˆ˜ 2](https://github.com/Seogeurim/Algorithm-practice/blob/master/src/Combination/P11051) | DP |
74-
| 03 | | [Baekjoon-1010 닀리 놓기](https://github.com/Seogeurim/Algorithm-practice/blob/master/src/Combination/P1010) | |
74+
| 03 | | [Baekjoon-1010 닀리 놓기](https://github.com/Seogeurim/Algorithm-practice/blob/master/src/Combination/P1010) | DP |
75+
| 04 | ⭐️ | [Baekjoon-1256 사전](https://github.com/Seogeurim/Algorithm-practice/blob/master/src/Combination/P1256) | |

β€Žsrc/Combination/P1256/Main.java

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package Combination.P1256;
2+
3+
import java.io.BufferedReader;
4+
import java.io.FileInputStream;
5+
import java.io.InputStreamReader;
6+
import java.util.StringTokenizer;
7+
8+
public class Main {
9+
10+
static int[][] combis = new int[202][202];
11+
static int MAX = 1000000000;
12+
13+
static int N, M, K;
14+
static int aCase, zCase;
15+
16+
public static void main(String[] args) throws Exception {
17+
// System.setIn(new FileInputStream("src/Combination/P1256/input.txt"));
18+
19+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
20+
StringTokenizer st = new StringTokenizer(br.readLine());
21+
22+
N = Integer.parseInt(st.nextToken());
23+
M = Integer.parseInt(st.nextToken());
24+
K = Integer.parseInt(st.nextToken());
25+
26+
if (K > comb(N + M, N)) {
27+
System.out.println(-1);
28+
} else {
29+
StringBuilder sb = new StringBuilder();
30+
while (N + M > 0) {
31+
if (N == 0 || M == 0) {
32+
break;
33+
}
34+
aCase = comb(N + M - 1, N - 1);
35+
zCase = comb(N + M - 1, M - 1);
36+
if (K <= aCase) {
37+
sb.append("a");
38+
N--;
39+
} else {
40+
sb.append("z");
41+
M--;
42+
K -= aCase;
43+
}
44+
}
45+
46+
for (int i = 0; i < N; i++) {
47+
sb.append('a');
48+
}
49+
for (int i = 0; i < M; i++) {
50+
sb.append('z');
51+
}
52+
System.out.println(sb);
53+
}
54+
}
55+
56+
static int comb(int n, int k) {
57+
if (combis[n][k] != 0)
58+
return combis[n][k];
59+
60+
for (int i = 1; i <= n; i++) {
61+
for (int j = 0; j <= k; j++) {
62+
if (i == j || j == 0){
63+
combis[i][j] = 1;
64+
} else {
65+
long result = combis[i-1][j-1] + combis[i-1][j];
66+
if (result > MAX)
67+
combis[i][j] = MAX;
68+
else
69+
combis[i][j] = (int) result;
70+
}
71+
}
72+
}
73+
74+
return combis[n][k];
75+
}
76+
}

β€Žsrc/Combination/P1256/README.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
🚩 λ‹€μŒ μ½”λ“œ κ°„κ²°ν™” μ‹œν‚€κΈ° (κ³„μ‚°ν•œ μˆ«μžκ°€ 10얡이 λ„˜μ–΄κ°€λ©΄ 10μ–΅μœΌλ‘œ μΉ˜ν™˜ν•΄μ£ΌλŠ” μ½”λ“œ)
3+
10μ–΅ : `1e9`둜 ν‘œν˜„ν•  수 μžˆλ‹€.
4+
5+
```java
6+
long result = combis[i-1][j-1] + combis[i-1][j];
7+
if (result > MAX)
8+
combis[i][j] = MAX;
9+
else
10+
combis[i][j] = (int) result;
11+
```
12+
13+
```java
14+
combis[i][j] = Math.min(combis[i-1][j-1] + combis[i-1][j], (int) 1e9);
15+
```

β€Žsrc/Combination/P1256/input.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2 3 7

0 commit comments

Comments
Β (0)