Skip to content

Commit e19a750

Browse files
committed
[String] baekjoon-1305
1 parent b1e2c96 commit e19a750

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

β€ŽREADME.md

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
| 01 | | [Baekjoon-14425 λ¬Έμžμ—΄ 집합](./src/String/P14425) | |
2929
| 02 | | [Baekjoon-14426 접두사 μ°ΎκΈ°](./src/String/P14426) | |
3030
| 03 | | [Baekjoon-1786 μ°ΎκΈ°](./src/String/P1786) | KMP |
31+
| 04 | | [Baekjoon-1305 κ΄‘κ³ ](./src/String/P1305) | KMP |
3132

3233
## Data Structure
3334

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

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package String.P1305;
2+
3+
import java.io.*;
4+
5+
public class Main {
6+
public static void main(String[] args) throws Exception {
7+
System.setIn(new FileInputStream("src/String/P1305/input.txt"));
8+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
9+
10+
int L = Integer.parseInt(br.readLine());
11+
String s = br.readLine();
12+
13+
int[] pattern = makePk(s);
14+
System.out.println(L - pattern[s.length()-1]);
15+
}
16+
17+
private static int[] makePk(String s) {
18+
int[] Pk = new int[s.length()];
19+
int k = 0;
20+
for (int i = 1; i < s.length(); i++) {
21+
while (k > 0 && s.charAt(i) != s.charAt(k))
22+
k = Pk[k-1];
23+
if (s.charAt(i) == s.charAt(k))
24+
Pk[i] = ++k;
25+
}
26+
return Pk;
27+
}
28+
}

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

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
## [baekjoon-1305] κ΄‘κ³ 
2+
3+
![image](https://user-images.githubusercontent.com/22045163/103282462-317f5b80-4a19-11eb-8d44-e6234669a85e.png)
4+
5+
### 풀이 κ³Όμ •
6+
7+
문제λ₯Ό λΆ„μ„ν•˜κΈ°λ₯Ό, μ•žμ„œ ν’€μ—ˆλ˜ [λ¬Έμžμ—΄ 검색](../P1786) λ¬Έμ œμ—μ„œλŠ”
8+
μ°ΎμœΌλ €λŠ” λ¬Έμžμ—΄ νŒ¨ν„΄μ΄ 주어지고, κ·Έ νŒ¨ν„΄μ— ν•΄λ‹Ήν•˜λŠ” λ¬Έμžμ—΄μ„ μ°ΎμœΌλ €λŠ”κ²Œ κ³Όμ œμ˜€λ‹€.
9+
ν•˜μ§€λ§Œ 이번 λ¬Έμ œμ—μ„œλŠ” λ°˜λŒ€λ‘œ **νŠΉμ • νŒ¨ν„΄μ΄ λ°˜λ³΅λ˜λŠ” λ¬Έμžμ—΄μ—μ„œ νŒ¨ν„΄μ„ μ°ΎλŠ” 것이 과제**μ˜€λ‹€.
10+
11+
μ‹œκ°„ νš¨μœ¨μ„ μƒκ°ν•΄λ³΄μ•˜μ„ λ•Œ, `L`의 μ΅œλŒ€ 크기가 100λ§Œμ΄λ―€λ‘œ 주어진 λ¬Έμžμ—΄μ„
12+
단 ν•œ 번만 `forλ¬Έ`으둜 ν›‘μ–΄μ„œ 닡을 λ„μΆœν•΄μ•Ό ν•œλ‹€. κ·Έλž¬μ„ λ•Œ λ‹€μŒκ³Ό 같은 생각이 μ΄μ–΄μ‘Œλ‹€.
13+
14+
![image](https://user-images.githubusercontent.com/22045163/103283797-dc454900-4a1c-11eb-98b5-4869db32bf2d.png)
15+
16+
μœ„μ™€ 같은 λ°©μ‹μœΌλ‘œ `ν˜„μž¬ κ²€μ‚¬ν•˜λ €λŠ” λ¬Έμžμ—΄ 길이`μ—μ„œ `접두사와 접미사가 같은 κ΅¬κ°„μ˜ 길이`λ₯Ό
17+
λΉΌμ£Όλ©΄ λ°˜λ³΅λ˜λŠ” λ¬Έμžμ—΄ νŒ¨ν„΄ 및 κ·Έ νŒ¨ν„΄μ˜ 길이λ₯Ό λ„μΆœν•  수 μžˆλ‹€.
18+
19+
이 λ•Œ, νŒ¨ν„΄μ΄ 3번 이상 λ°˜λ³΅λ˜λŠ” κ²½μš°λŠ”?
20+
21+
![image](https://user-images.githubusercontent.com/22045163/103283950-3e9e4980-4a1d-11eb-930a-23423f59290c.png)
22+
23+
λ§ˆμ°¬κ°€μ§€λ‘œ μœ„μ™€ 같이 같은 λ°©μ‹μœΌλ‘œ ꡬ할 수 μžˆλ‹€.
24+
25+
**KMP μ•Œκ³ λ¦¬μ¦˜**μ—μ„œ `makePk` ν•¨μˆ˜λŠ” λ¬Έμžμ—΄μ—μ„œ 접두사와 접미사가 같을 수 μžˆλŠ”
26+
μ΅œλŒ€μ˜ 길이λ₯Ό 배열에 μ €μž₯ν•œλ‹€. 이 ν•¨μˆ˜λ₯Ό 톡해 λ„μΆœλ˜λŠ” 값을 μ΄μš©ν•˜λ©΄ μ›ν•˜λŠ” 닡을 찾을 수 μžˆλ‹€.

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

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
5
2+
aaaaa
3+
output 1
4+
5+
11
6+
aabaaabaaab
7+
output 4
8+
9+
6
10+
aabaaa
11+
output 4

0 commit comments

Comments
Β (0)