String: KMP algorithm

KMP algorithm first looks a little complicated, but the principle is very simple, to think clearly understood.

Here is a simple chestnut:

Main string s: abcabfd

Pattern string t: abcabe

If primitive method, a character is performed by comparing the main string to be i = 5, s [5] = f position, and found that j = 5, t [5] = e do not match, then only start again from i = 2, then a match j =.

In fact, due to the characteristics of the pattern string: ab & C ab & E, there are two ab & the string, while the front also has a main string ab & f, then we can directly f and j = 3, t [3] = c for in contrast, because the front of ab is the same, no more than once compared.

So actually KMP algorithm utilizes the characteristics of the pattern string, if the string has a larger pattern repeat, the determination may be omitted many, because in at least the main stream i is not moving, not the original image as backtracking algorithm.

 

KMP algorithm is divided into two parts:

The main body 1) .KMP of

2) .next array

 

1) The effect of the two strings is pattern matching, but 1) need to use next array 2), and therefore the first to build a next array before performing matching.

Effect similar to the above on the next array chestnut, when it is judged to s [5] is not equal to t [5], a direct lookup next [5], next [5] f should be directed is relatively a character, here next [5] = 2, as F ab ab bEFORE equal to e are known, so from t [2] can be directly compared with each other.

 

next array of building:

Consider the pattern string t, i and j pattern string position:

If t [i] = t [j], it is known that (1 ~ i) exactly equal between the (j-i + 1 ~ j), then for next [j + 1], i.e. when the main string does strings Model of equal j + 1 characters, the comparison should be the next i-th element, which is next [j + 1] = i + 1;

If t [i]! = T [j], but found that (1 ~ i-1) and (j-i + 1 ~ j-1) between exactly equal, then the j-th element, should first and the second Next [j ] contrast elements once again, this time if equal, the next [j + 1] = next [j] +1;

Otherwise, continue to find the next one, which is the first next [next [j]] elements.

 

After obtaining the next array, the remaining KMP body is very simple, as long as the elements i and j corresponding contrast, if s [i] == s [j], then i ++, j ++; otherwise, the next i and [j] elements contrast ......

Code:

The code may be divided into two parts:

KMP part of the body to create an array of another part of the next.

In addition, to create the next part of the array of so:

Their role is to:

Pattern strings such as: aab

When creating next, next [0] is equal to -1 nature, and Next [1], without this part, will be set to 0, i.e. if the main string and a string pattern portion is not equal to i = 1, also go t [0] contrast, in fact, t [0] and t [1] is a, and therefore it is not necessary, should be provided directly to -1.

if(t[i]!=t[j])
	next[i]=j;
else
	next[i]=next[j];


class Solution
{
public:
	int KMP(string s,string t)
	{
		vector<int> next=get_next(t);
		int i=0;int j=0;
		while(i<s.size()&&j<t.size())
		{
			if(j==-1||s[i]==s[j])
			{
				i++;
				j++;
			}
			else
				j=next[j];
		}
		if(j<t.size())
			return 0;
		else
			 return i;
	}
	vector<int> get_next(string t)
	{
		vector<int> next(t.size(),-1);
		int i=0;int j=-1;
		while(i<(t.size()-1))
		{
			if(j==-1||t[i]==t[j])
			{
				i++;
				j++;
				if(t[i]!=t[j])
					next[i]=j;
				else
					next[i]=next[j];
			}
			else
				j=next[j];
		}
		return next;
	}
};

  

 

Guess you like

Origin www.cnblogs.com/lxy-xf/p/11233420.html