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

+6
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

+27
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

+42
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

+17
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)