KMP模式匹配算法,求next数组总是不对,找了半天找不到问题,测试的字符串为abcabx,正确的next数组应该是0011123,程序结果却是0011121

Tiger_shl 2017-11-29 03:17:12
public class Test{
public static void main(String[] args) {
String T = "abcabx";
int[] next_1 = new int[T.length()+1];
int[] next = get_next(T,next_1);
for(int e : next){
System.out.println(e);
}
}

//求解next数组
public static int[] get_next(String T,int[] next){
int i,j;
i = 1;
j = 0;
next[1] = 0;
while(i < T.length()){
if(j == 0 || T.charAt(i) == T.charAt(j)){ //比较后缀与前缀的字符
++i;
++j;
next[i] = j;
}else{
j = next[j]; //若字符不相等,则j值回溯
}
}
return next;
}
}
...全文
126 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

3,423

社区成员

发帖
与我相关
我的任务
社区描述
其他开发语言 其他开发语言
社区管理员
  • 其他开发语言社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧