码迷,mamicode.com
首页 > 编程语言 > 详细

浅谈next数组

时间:2015-06-30 21:58:43      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:kmp

next数组是加速字符串匹配的的一个重要工具


以下是next数组的实现过程

int* getnext(string &T)
{
	int length = T.length();
	int *next = new int[length];
	next[0] = -1;
	next[1] =  0;
	
	int i = 1;
	int j = 0;
	while(i<length)
	{
		if(T[i] == T[j])
		{
			next[++i] = ++j;
		}
		else if(j>0)
		{
			 j = next[j];
		}
		else
		{
			next[++i] = 0;
		}
	}
	return next;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

浅谈next数组

标签:kmp

原文地址:http://blog.csdn.net/chenmengmengx/article/details/46700927

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!