KMP算法中next数组的理解与算法的实现(java语言)

时间:2022-07-06 06:14:00

KMP 算法我们有写好的函数帮我们计算 Next 数组的值和 Nextval 数组的值,但是如果是考试,那就只能自己来手算这两个数组了,这里分享一下我的计算方法吧。

计算前缀 Next[i] 的值:

我们令 next[0] = -1 。从 next[1] 开始,每求一个字符的 next 值,就看它前面是否有一个最长的"字符串"和从第一个字符开始的"字符串"相等(需要注意的是,这2个"字符串"不能是同一个"字符串")。如果一个都没有,这个字符的 next 值就是0;如果有,就看它有多长,这个字符的 next 值就是它的长度。

计算修正后的 Nextval[i] 值:

我们令 nextval[0] = -1。从 nextval[1] 开始,如果某位(字符)与它 next 值指向的位(字符)相同,则该位的 nextval 值就是指向位的 nextval 值(nextval[i] = nextval[ next[i] ]);如果不同,则该位的 nextval 值就是它自己的 next 值(nextvalue[i] = next[i])。

举个例子:

KMP算法中next数组的理解与算法的实现(java语言)

计算前缀 Next[i] 的值:

next[0] = -1;定值。
next[1] = 0;s[1]前面没有重复子串。
next[2] = 0;s[2]前面没有重复子串。
next[3] = 0;s[3]前面没有重复子串。
next[4] = 1;s[4]前面有重复子串s[0] = 'a'和s[3] = 'a'。
next[5] = 2;s[5]前面有重复子串s[01] = 'ab'和s[34] = 'ab'。
next[6] = 3;s[6]前面有重复子串s[012] = 'abc'和s[345] = 'abc'。
next[7] = 4;s[7]前面有重复子串s[0123] = 'abca'和s[3456] = 'abca'。

计算修正后的 Nextval[i] 值:

nextval[0] = -1;定值。
nextval[1] = 0;s[1] != s[0],nextval[1] = next[1] = 0。
nextval[2] = 0;s[2] != s[0],nextval[2] = next[2] = 0。
nextval[3] = -1;s[3] == s[0],nextval[3] = nextval[0] = -1。
nextval[4] = 0;s[4] == s[1],nextval[4] = nextval[1] = 0。
nextval[5] = 0;s[5] == s[2],nextval[5] = nextval[2] = 0。
nextval[6] = -1;s[6] == s[3],nextval[6] = nextval[3] = -1。
nextval[7] = 4;s[7] != s[4],nextval[7] = next[7] = 4。

next[] 数组的定义为(相对C++):最后程序算法是用JAVA
next[1]= 0;
next ( i>=0 ) 为:  对于模式串 S[],  我们用 S[i, j] 表示从 S[] 中 i 到 j 这一段子串。 找一个 k(0< k< i ) 使得,S[0, k-1] 与 S[i- k, i-1] 完全匹配,并且 k 的值最大, 那么 next= k; 如果不存在完全匹配的,next= 0;
               1 2 3 4 5 6 7 8
如对于串    a b a a b c a c ,  next[1]= 0, 显然 next[2]= 1;
i== 3 时, 满足 0< k< 2 的 k 只有 1, 而 S[0, 0] 与 S[1,1] 不完全匹配,所以 next[3]= 1;
i== 4 时, 满足 0< k< 3 的 k 有 1, 2,  S[0,1] 'ab' 与 S[1,2] 'ba' 不完全匹配,S[0,0] 'a' 与 S[2,2] ’a' 完全匹配, 故 next[4]= 2;
i== 5 时,  S[0,2] ’aba' != S[1,3] 'baa'      S[0,1] 'ab'!= S[2,3] 'aa'  S[0,0]== S[3,3],故 next[5]= 2;
同理: next[6]= 3, next[7]= 1, next[8]= 2;

可以用递推的方法求得 next[] 数组
假设 next = k, 则有 S[0, k-1]== S[i-k, i-1], 如何求 next[i+1]?
1). 如果 S[k]== S[i+1], 则有 S[0,k]== S[i-k, i],  可以肯定,不可能存在 x> k, 使得 S[0, x]== S[i-x, i] 故 next[i+1]= next+ 1= k+ 1;
2). 如果 S[k]!= S[i+1], 可看成是一个模式匹配问题,相当于 S[len(S)-K, len(S)]为主串, S[0, len(S)]  为模式串的模式匹配,这时应将模式串向右滑动 next[k] 个字符, 再比较 S[ next[k] ]与 S[i+1], 如果相等, 则 next[i+1]= next[ next[k] ]+ 1, 如此继续。

  1 public class KMP
2 {
3 // KMP中的核心算法,获得记录跳转状态的next数组
4 public static int[] next(String sub)
5 {
6 int[] next = new int[sub.length() + 1];
7 char[] T = sub.toCharArray();
8 int length = sub.length();
9
10 int i, j;
11 i = 1;
12 j = 0;
13 next[1] = 0;//第一个字符的next值是0, 假设数组next是从1开始的算,next[0]不用
14 while (i < length)
15 {
16 if (j == 0 || T[i - 1] == T[j - 1])// 此处T[i]表示后缀单个字符,T[j]表前缀
17 {
18 ++i;
19 ++j;
20 next[i] = j;//存放当前的next值为此时模式串的游标值
21 } else
22 j = next[j];// 若字符不同,刚j值回溯
23 //System.out.println("i:" + i + " j:" + j);
24 }
25 return next;
26 }
27
28 public static int[] nextval(String sub)
29 {
30 int[] next = new int[sub.length() + 1];
31 char[] T = sub.toCharArray();
32 int length = sub.length();
33
34 int i, j;
35 i = 1;
36 j = 0;
37 next[1] = 0;
38 while (i < length)
39 {
40 if (j == 0 || T[i - 1] == T[j - 1])// 此处T[i]表示后缀单个字符,T[j]表前缀
41 {
42 ++i;
43 ++j;
44
45 if (T[i - 1] != T[j - 1]) // 若当前字符与前缀字符不同
46 {
47 next[i] = j;
48 } else
49 {
50 next[i] = next[j]; // 若相同,刚将前缀字符的next值赋给next在i位置的值
51 }
52
53 } else
54 j = next[j];// 若字符不同,刚j值回溯
55 //System.out.println("i:" + i + " j:" + j);
56 }
57 return next;
58 }
59
60 public static int Index_KMP(String str, String sub, int pos)
61 {
62 char[] S = str.toCharArray();
63 char[] T = sub.toCharArray();
64 int[] next = next(sub);
65 int i;
66 if (pos < 1)
67 {
68 System.out.println("起位置输入错误,默认设置为1");
69 i = 1;
70 } else
71 {
72 i = pos;
73 }
74 int j = 1; // i控制S,j控制T;
75 while (i <= S.length && j <= T.length)
76 {
77 if (j == 0 || S[i - 1] == T[j - 1])
78 {
79 ++i;
80 ++j;
81 } else
82 {
83 j = next[j]; // j退回合适的位置,i值不变
84 }
85 }
86 if (j > T.length)
87 return i - T.length;
88 else
89 return 0;
90 }
91
92 public static void main(String[] args)
93 {
94 String sub = "abaabcac";
95 String str = "zzzaabaabcacfdsafas";
96 int[] next = next(sub);
97 System.out.print("next数组是:");
98 for (int i = 1; i < next.length; i++)
99 {
100 System.out.print(next[i]);
101 }
102
103 int index = Index_KMP(str, sub, 1);
104 System.out.print("\n匹配位置为:" + index);
105 }
106 }