-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKMP.java
More file actions
44 lines (36 loc) · 855 Bytes
/
Copy pathKMP.java
File metadata and controls
44 lines (36 loc) · 855 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package dummypkg;
import java.util.Scanner;
public class KMP {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s=new Scanner(System.in);
String pat=s.nextLine();
String text=s.nextLine();
int LPS[]=new int[pat.length()];
buildLPS(pat, LPS);
printArray(LPS, pat.length());
}
public static void printArray(int[] a, int n) {
for(int i=0; i<n; i++) {
System.out.print(a[i]+" ");
}
System.out.println();
}
public static void buildLPS(String pat, int[] LPS) {
LPS[0]=0;
int j=0, len=pat.length();
for(int i=1; i<len; i++) {
if(pat.charAt(i)==pat.charAt(j)) {
LPS[i]=++j;
} else {
while(j!=0 && pat.charAt(i)!=pat.charAt(j)) {
j=LPS[j-1];
}
if(j==0)
LPS[i]=0;
else
LPS[i]=++j;
}
}
}
}