-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKMPTest.java
More file actions
80 lines (66 loc) · 1.76 KB
/
KMPTest.java
File metadata and controls
80 lines (66 loc) · 1.76 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package string;
//字符串匹配算法
public class KMPTest
{
public static void main(String[] args)
{
KMP kmp = new KMP();
String s ="aaaaaaaaaaaaabaaaaaaaaaaaaaabcdefd";
String t ="aaaaaab";
System.out.println(kmp.indexKMP(s, t, 1));
}
}
class KMP
{
public void getNext(String t, int[] next)
{
char[] t1 =(" " +t).toCharArray(); //用来做后面的字符串比较,但第一个字符的标好为1,故加一个空格
int i=1;
int j=0;
next[1] = 0;
while(i < t.length())
{
if( j == 0 || t1[i] ==t1[j])
{
i++;
j++;
next[i] = j;
//----------------改进的部分-----------
if(t1[i] != t1[j]) //若当前字符与前缀字符不同
next[i] =j; //则当前的j为next在i位置的值
else //如果与前缀字符相同,则将前缀字符的next值赋给next在i位置的值
next[i] = next[j];
}
else
j = next[j]; //若字符不相同,则j值回溯
}
}
//返回子串t在主串s中第pos个字符之后的位置,若不存在,则函数返回值为0
//T非空, 1<= pos <=s.length();
public int indexKMP(String s, String t, int pos)
{
int i =pos;
int j =1;
int[] next = new int[255]; //定义一个next数组
//比较的时候都是从数组的位置1开始的,故将位置0用空格填充
char[] s1 = (" "+ s).toCharArray();
char[] t1 = (" " + t).toCharArray();
getNext(t,next);
while(i<= s.length() && j<= t.length()) //若i小于s的长度且j小于t的长度,循环继续
{
if(j ==0 || s1[i] == t1[j])
{
i++;
j++;
}
else //指针后退重新开始匹配
{
j = next[j]; //j退回合适的位置,i值不变
}
}
if(j > t.length())
return i - t.length();
else
return 0;
}
}