有一串首尾相连的珠子,共有m个,每一个珠子有一种颜色,并且颜色的总数不超过n(n<=10),求连续的珠子的颜色总数为n时,长度最小的区间。

#include<iostream>usingnamespacestd;#defineMAXN10intcolors[MAXN];//recordthecounterofonecolorintcolorsCounter;voidfind(intarr[],intlen,intcolorsNeed){intbestStartIndex=0;intbestLen=len;intlastStartIndex=0;for(inti=0;i<len;++i){if(!colors[arr[i]])colorsCounter++;colors[arr[i]]++;if(colorsCounter==colorsNeed){intj=lastStartIndex;while(colors[arr[j]]>1){colors[arr[j]]--;++j;}if(i-j+1<bestLen){bestStartIndex=j;bestLen=i-j+1;if(bestLen==colorsNeed)break;}lastStartIndex=j;}}