sunday算法的实现

github上的一个sunday算法的实现。

原文地址https://github.com/coderchen/leetcode/b

#include <iostream>
#include <string>
using namespace std;
int sunday(string match, string pattern)
{
	int m = match.size();
	int n = pattern.size();
	int str[256];
	for (int i = 0; i < 256; i++)
	{
		str[i] = -1;
	}
	for (int i = 0; i < n; i++)
	{
		str[(int)pattern[i]] = i;
	}
	for (int i = 0; i < m-n;)
	{
		int j = 0;
		while (j < n)
		{
			if (match[i] == pattern[j])
			{
				i++;
				j++;
			}
			else
			{
				int p = n + i - j;
				if (str[match[p]] == -1)
					i = i + n;
				else
					i = i + n - str[match[p]];
				break;
			}
		}
		if (j == n)
			return i - n;

	}
	return -1;
}

lob/master/Implement_strStr.cpp

点赞