Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8468c48

Browse filesBrowse files
committed
Update
1 parent a7c10c4 commit 8468c48
Copy full SHA for 8468c48

3 files changed

+92-3Lines changed: 92 additions & 3 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎README.md‎

Copy file name to clipboardExpand all lines: README.md
+1-1Lines changed: 1 addition & 1 deletion
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
* 주어진 배열에서 합이 최대가 되는 sub array의 합을 구한다. [code](https://github.com/JaeYeopHan/algorithm_basic_java/blob/master/src/test/java/exercise/FindMaxSumInArray.java)
9393

9494
### Famous Algorithm
95-
* Karp_Rabin_Algorithm [code]()
95+
* Karp_Rabin_Algorithm [code](https://github.com/JaeYeopHan/algorithm_basic_java/blob/master/src/test/java/famous_algorithm/Karp_Rabin_Algorithm.java)
9696

9797
</br>
9898

Collapse file
+85Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package famous_algorithm;
2+
3+
import org.junit.Test;
4+
5+
import static org.hamcrest.CoreMatchers.is;
6+
import static org.junit.Assert.assertThat;
7+
8+
public class KMP_Algorithm {
9+
10+
/*
11+
TASK
12+
장문의 문자열 A가 존재할 때,
13+
이 문자열 A 안에 특정 문자열 B가 존재하는지 알 수 있는 방법을 해결한다.
14+
*/
15+
16+
@Test
17+
public void test() {
18+
assertThat(KMP("abcxabcdabcdabcy".toCharArray(), "abcdabcy".toCharArray()), is(true));
19+
}
20+
21+
/*
22+
SOLVE
23+
Karp-Rabin에서는 한 칸씩 이동하면서 패턴 문자열과 비교해줬다.
24+
하지만 이는 비교 과정 중에 발생한 소중한 정보를 버리고 다시 비교하는 것이다.
25+
kmp는 한 칸씩 이동하는 것이 아니라 몇 칸씩 이동하며 비교하기 때문에
26+
karp-rabin 보다 빠르게 탐색이 가능하다.
27+
28+
패턴의 접두사와 접미사 그리고 경계라는 것을 사용하여 비교가 필요없는 경우를 필터링해 필요할 때만 비교를 해준다.
29+
비교를 한 다음 일치하는 부분의 접두사와 접미사를 비교하여 같은 개수 만큼 이동한다.
30+
그리고 경계부터 다시 본문과 비교해준다.
31+
32+
매번 접두사와 접미사를 비교하는 것도 비용이므로
33+
접두사와 접미사가 같은 개수에 대한 테이블을 만들어둔다.
34+
*/
35+
36+
private int[] computeTemporaryArray(char[] pattern) {
37+
int[] lps = new int[pattern.length];
38+
int idx = 0;
39+
for (int i = 1; i < pattern.length;) {
40+
if (pattern[i] == pattern[idx]) {
41+
lps[i] = idx + 1;
42+
idx++;
43+
i++;
44+
} else {
45+
if (idx != 0) {
46+
idx = lps[idx - 1];
47+
} else {
48+
lps[i] = 0;
49+
i++;
50+
}
51+
}
52+
}
53+
return lps;
54+
}
55+
56+
public boolean KMP(char []text, char []pattern){
57+
58+
int lps[] = computeTemporaryArray(pattern);
59+
int i=0;
60+
int j=0;
61+
while(i < text.length && j < pattern.length){
62+
if(text[i] == pattern[j]){
63+
i++;
64+
j++;
65+
}else{
66+
if(j!=0){
67+
j = lps[j-1];
68+
}else{
69+
i++;
70+
}
71+
}
72+
}
73+
if(j == pattern.length){
74+
return true;
75+
}
76+
return false;
77+
}
78+
79+
/*
80+
REFERENCE
81+
YOUTUBE : https://www.youtube.com/watch?v=GTJr8OvyEVQ
82+
GITHUB : https://github.com/mission-peace/interview/blob/master/src/com/interview/string/SubstringSearch.java
83+
*/
84+
85+
}
Collapse file

‎src/test/java/famous_algorithm/Karp_Rabin_Algorithm.java‎

Copy file name to clipboardExpand all lines: src/test/java/famous_algorithm/Karp_Rabin_Algorithm.java
+6-2Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class Karp_Rabin_Algorithm {
1515

1616
@Test
1717
public void test() {
18-
RabinKarpSearch rbk = new RabinKarpSearch();
18+
KarpRabinSearch rbk = new KarpRabinSearch();
1919
assertThat(rbk.patternSearch("dgethwabcafg".toCharArray(), "abc".toCharArray()), is(6));
2020
}
2121

@@ -28,9 +28,11 @@ public void test() {
2828
이 때 핵심은 찾고자 하는 문자열의 hash 값은 물론이고 본문의 hash 값을 효율적으로 구할 수 있는 것이다.
2929
즉 찾고자하는 문자열과 비교를 할 때 매번 hash 값을 생성하는 것이 아니고
3030
수학적인 규칙 속에서 이미 계산한 값을 재사용하는 것이 핵심이다.
31+
32+
TIME COMPLEXITY : O(MN)
3133
*/
3234

33-
public class RabinKarpSearch {
35+
public class KarpRabinSearch {
3436
private int prime = 101;
3537

3638
public int patternSearch(char[] text, char[] pattern) {
@@ -81,9 +83,11 @@ private long createHash(char[] str, int end) {
8183
return hash;
8284
}
8385
}
86+
8487
/*
8588
REFERENCE
8689
YOUTUBE : https://www.youtube.com/watch?v=H4VrKHVG5qI
8790
GITHUB : https://github.com/mission-peace/interview/blob/master/src/com/interview/string/RabinKarpSearch.java
8891
*/
92+
8993
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.