#include <iostream>
using namespace std;
#define N 3
bool GetMinlen(int *a,int m,int &shortHead,int & shortlen)
{
int start=0,end = 0;
int countColor = 0;
int *colorArray = new int[N];
shortlen = m;
shortHead = 0;
for(int i =0;i< N;i++)
{
colorArray[i] = 0;
}
while(start < m)
{
//向后搜索直至所有的颜色均出现
while(countColor < N && (end+1)%m !=start)
{
if(colorArray[a[end]]++ == 0)
{
countColor++;
}
if(countColor <N)
{
end=(end+1)%m;
}
}
if(countColor != N)
{
cout << "珠子颜色数少于所要找的数目"<<endl;
return false;
}
//start向后移动,直到有一个颜色数为0
while(start<m)
{
if(colorArray[a[start]] > 1)
{
colorArray[a[start]]--;
start++;
}
else
{
break;
}
}
if(shortlen > end - start +1)
{
shortlen = end -start +1;
shortHead = start;
}
colorArray[a[start]]--;
start++;
countColor--;
end = (end+1)%m;
colorArray[a[end]]++
一串首尾相连的珠子(m个),有N种颜色(N<=10),设计一个算法,取出其中一段,要求包含所有N中颜色,并使长度最短
最新推荐文章于 2022-05-27 19:26:13 发布