Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions ShinHeeEul/202502/26 BOJ P5 찾기.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
```java
import java.io.*;
import java.util.*;

public class Main {

static int[] failure;
static int count = 0;
static int pLength;
static int tLength;
static StringBuilder sb;

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

char[] T = br.readLine().toCharArray();
char[] P = br.readLine().toCharArray();

pLength = P.length;
tLength = T.length;

failure = new int[P.length];
sb = new StringBuilder();
failureFunction(P);
KMP(T, P);

System.out.println(count);
System.out.println(sb);
}

public static void failureFunction(char[] s) {

int pIdx = 0;

for(int index = 1; index < pLength; index++) {

while(pIdx != 0 && s[index] != s[pIdx]) pIdx = failure[pIdx - 1];

if(s[pIdx] == s[index]) {
pIdx++;
failure[index] = pIdx;
}
}

}

public static void KMP(char[] t, char[] p) {
int pIdx = 0;

for(int index = 0; index < tLength; index++) {

while(pIdx != 0 && t[index] != p[pIdx]) pIdx = failure[pIdx - 1];

if(t[index] == p[pIdx]) {
if(pIdx == pLength - 1) {
sb.append(index - pLength + 2).append(" ");
count++;
pIdx = failure[pIdx];
} else {
pIdx++;
}
}
}

}

}
```