Skip to content

Commit 5054c79

Browse files
committed
[String] baekjoon-14425
1 parent 98b575e commit 5054c79

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

β€ŽREADME.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,12 @@
237237
| 27 | | [Baekjoon-1699 제곱수의 ν•©](src/DP/P1699) | |
238238
| 28 | | [Baekjoon-2225 ν•©λΆ„ν•΄](src/DP/P2225) | |
239239

240+
## String
241+
242+
| # | β˜† | Problem | Note |
243+
| :-: | :-: | :------------------------------------------------- | :----- |
244+
| 01 | | [Baekjoon-14425 λ¬Έμžμ—΄ μ§‘ν•©](./src/String/P14425) | |
245+
240246
---
241247

242248
## 2020 KAKAO BLIND RECRUITMENT

β€Žsrc/String/P14425/Main.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package String.P14425;
2+
3+
import java.io.*;
4+
import java.util.*;
5+
6+
public class Main {
7+
8+
static int N, M;
9+
10+
public static void main(String[] args) throws Exception {
11+
System.setIn(new FileInputStream("src/String/P14425/input.txt"));
12+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
13+
StringTokenizer st = new StringTokenizer(br.readLine());
14+
15+
N = Integer.parseInt(st.nextToken());
16+
M = Integer.parseInt(st.nextToken());
17+
18+
HashSet<String> hs = new HashSet<>();
19+
while (N-- > 0) hs.add(br.readLine());
20+
21+
int ans = 0;
22+
for (int i = 0; i < M; i++)
23+
if (hs.contains(br.readLine())) ans ++;
24+
25+
System.out.println(ans);
26+
}
27+
}

β€Žsrc/String/P14425/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
## [baekjoon-14425] λ¬Έμžμ—΄ μ§‘ν•©
2+
3+
![image](https://user-images.githubusercontent.com/22045163/102742413-2e8cc700-4398-11eb-8039-00a6f1aaa102.png)
4+
5+
### 더 효율적인 μ•Œκ³ λ¦¬μ¦˜ μƒκ°ν•˜κΈ°
6+
7+
λ³Έ λ¬Έμ œλŠ” λŒ€μƒ λ¬Έμžμ—΄μ΄ λ¬Έμžμ—΄ μ§‘ν•© 내에 μžˆλŠ”μ§€ 일일이 κ²€μ‚¬ν•΄λ³΄λŠ” μˆ˜λ°–μ— μ—†λ‹€.
8+
9+
μ²˜μŒμ—λŠ” λ‹€μŒκ³Ό 같이 `O(N*M)` 으둜 ν’€μ—ˆλ‹€. μ‹œκ°„ μ œν•œμ΄ 2초이고,
10+
`N`κ³Ό `M`의 μ΅œλŒ“κ°’μ€ `10000`이기 λ•Œλ¬Έμ— 문제 μ—†μ—ˆλ‹€.
11+
12+
```java
13+
String S = new String[N];
14+
for (int i = 0; i < N; i++) S[i] = br.readLine();
15+
16+
int ans = 0;
17+
for (int i = 0; i < M; i++) {
18+
String s = br.readLine();
19+
for (int j = 0; j < N; j++) {
20+
if (s.equals(S[j])) ans ++;
21+
}
22+
}
23+
```
24+
25+
이 λ•Œ 닡은 λ§žμ•˜μœΌλ‚˜ μ‹œκ°„ 효율이 μ’‹μ§€ μ•Šμ€ 것 κ°™μ•„ λ‹€λ₯Έ μ½”λ“œλ₯Ό μ°Έκ³ ν•˜μ˜€κ³ ,
26+
Java Collections의 **HashSet**을 μ΄μš©ν•˜λ©΄ μ‹œκ°„ λ³΅μž‘λ„λ₯Ό 크게 쀄일 수 μžˆλ‹€λŠ” 것을 μ•Œκ²Œ λ˜μ—ˆλ‹€.
27+
28+
**HashSet**의 `Add`, `Contains` μ—°μ‚°μ˜ μ‹œκ°„ λ³΅μž‘λ„λŠ” λͺ¨λ‘ **O(1)** 이닀.
29+
λ”°λΌμ„œ λ‹€μŒκ³Ό 같이 μ½”λ“œλ₯Ό μž‘μ„±ν•˜λ©΄ 더 쒋은 μ½”λ“œκ°€ 될 수 μžˆκ² μ§€ !!
30+
31+
```java
32+
HashSet<String> hs = new HashSet<>();
33+
while (N-- > 0) hs.add(br.readLine());
34+
35+
int ans = 0;
36+
for (int i = 0; i < M; i++)
37+
if (hs.contains(br.readLine())) ans ++;
38+
```
39+
40+
![image](https://user-images.githubusercontent.com/22045163/102742722-ec17ba00-4398-11eb-8bc9-479a07214da8.png)
41+
42+
μ‹œκ°„ λ³΅μž‘λ„λ₯Ό `2648ms` μ—μ„œ `424ms`둜 쀄일 수 μžˆλ‹€.

β€Žsrc/String/P14425/input.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
5 11
2+
baekjoononlinejudge
3+
startlink
4+
codeplus
5+
sundaycoding
6+
codingsh
7+
baekjoon
8+
codeplus
9+
codeminus
10+
startlink
11+
starlink
12+
sundaycoding
13+
codingsh
14+
codinghs
15+
sondaycoding
16+
startrink
17+
icerink

0 commit comments

Comments
Β (0)