-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
66 lines (62 loc) · 2.05 KB
/
Copy pathMain.java
File metadata and controls
66 lines (62 loc) · 2.05 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.util.*;
public class Main {
public static void main(String[] args){
System.out.println(kmpSearch("abaacababcac","ac"));
}
public static ArrayList<Integer> GetJumpStep(String pattern) {
ArrayList<Integer> steps = new ArrayList<>();
steps.add(-1);
int len = pattern.length();
ArrayList<String> substrings = new ArrayList<String>(len - 1);
int count = 1;
for (count = 1; count < len; count++) {
substrings.add(pattern.substring(0, count));
}
for (count=0; count<substrings.size();count++){
String prefix;
String suffix;
int max = 0;
String a = substrings.get(count);
for (int countt = 1;countt<a.length();countt++){
prefix=a.substring(0,countt);
suffix=a.substring(a.length()-countt);
if(prefix.equals(suffix)){
max = countt;
}
}
steps.add(max);
}
return steps;
}
public static ArrayList<Integer> kmpSearch (String s,String p){
int i = 0;
int j = 0;
int sLen = s.length();
int pLen = p.length();
ArrayList<Integer> targetPositon = new ArrayList<Integer>();
while(true){
j = 0;
System.out.println("found");
while (j < pLen) {
if (j == -1 || s.charAt(i) == p.charAt(j))
{
i++;
j++;
}
else
{
j = GetJumpStep(p).get(j);
if(pLen-j-1>=sLen-i-1){
if(s.substring(sLen-pLen).equals(p)){
targetPositon.add(i - j);
}
return targetPositon;
}
}
}
targetPositon.add(i - j);
System.out.println("found"+i+" "+j);
System.out.println("found"+(pLen-j-1)+" "+(sLen-i-1));
}
}
}