admin管理员组

文章数量:1558102

http://haixiaoyang.wordpress/2012/03/25/find-the-smallest-window-of-a-certain-sequence/

//Given an input array of integers of size n, and a query array of 
//integers of size k, find the smallest window of input array that 
//contains all the elements of query array and also in the same order

int GetMinOrder(int a[], int n, int b[], int m)
{
	assert(a && n > 0 && b && m > 0 && n >= m);

	int nMin = -1;
	for (int i = 0; i < n; i++)
	{
		if (a[i] != b[0]) continue;

		int nCur = 0;
		int j = i;
		for (; j < n && nCur < m; j++)
		{
			if (a[j] == b[nCur])
				nCur++;
		}

		if (nCur == m)
			nMin = nMin < 0 ? (j - i) : min(nMin, (j-i));
		else break;
	}

	return nMin;
}


本文标签: smallestFindsequencewindow