您的位置:首页 > 其它

一串首尾相连的珠子(m 个),有N 种颜色(N<=10),设计一个算法,取出其中一段,要求包含所有N 中颜色,并使长度最短。并分析时间复杂度与空间复杂度。

2012-12-15 22:18 1216 查看
思路:先从index=0处搜索,每检查一颗珠子,响应的颜色数量+1,如果是新的颜色则总颜色数+1.

当颜色总数为n时,找到第一个满足条件的连续序列。

1>从该序列起始处搜索,若搜索处的颜色数量不为1,则表明该串还有别的珠子有该颜色,继续往前搜索并更新该序列,起始索引位置+1.

若搜索处颜色数量为1,停止搜索。

2>比较最佳序列长与当前序列长,更新最佳序列。记录当前序列起始位置。

从第一个满足条件的序列继续index++,并检查1,2条件。

如果不是首尾相连的:
#include <iostream>
using namespace std;

#define MAXN 10
int colors[MAXN];//record the counter of one color
int colorsCounter;
void find(int arr[],int len, int colorsNeed)
{
    int bestStartIndex = 0;
    int bestLen = len;
    int lastStartIndex = 0;
    
    for ( int i=0; i<len; ++i) 
    {
        if (!colors[arr[i]])//如果当前数字还没有被记录,则说明又找到一个新的 
            colorsCounter++;
        colors[arr[i]]++;

        if (colorsCounter==colorsNeed) //需要的个数已经查找够了 
        {
            int j = lastStartIndex;
            while (colors[arr[j]]>1) //直到当前 元素arr 在以后的子序列中不再出现 
            {
                colors[arr[j]]--;
                ++j; //查找下一个arr[]颜色,看是否以后还存在 
            }
            
            if (i-j+1<bestLen) //当前子序列小于当前的最佳长度 
            {
                bestStartIndex = j;//记录开始位置 
                bestLen = i-j+1;   //记录最佳长度 
                if (bestLen==colorsNeed)//如果正好等于需要颜色数,则说明每一个正好一个。则退出 
                    break;
            }
            lastStartIndex = j;//记录本次开始的位置 
        }
    }

    cout << "bestStartIndex: " <<bestStartIndex<< endl;
    cout << "bestLen: " <<bestLen<< endl;
    for (int i=bestStartIndex; i<bestLen+bestStartIndex; ++i)
        cout << arr[i] << " ";
    cout << endl;
}

int main()
{
    int arr[] = {1,2,3,3,2,1,4,1,3,4,5,5,6,2,3,4,4,1,5,2,3,4};
    int m = sizeof(arr)/sizeof(arr[0]);

    for ( int i=0; i<m; ++i)
        cout << arr[i] <<" ";
    cout << endl;

    int n = 6;
    find(arr,m,n);

    return 0;
}




如果带环的话:

再多加一次循环,从上一次得到最小长度的地方开始,到开始阶段,第一次满足所有元素的地方。

#include <iostream>
using namespace std;

#define MAXN 10
int colors[MAXN];//record the counter of one color
int colorsCounter;
void find(int arr[],int len, int colorsNeed)
{
    int bestStartIndex = 0;
    int bestLen = len;
    int lastStartIndex = 0;
    int firstFoundEnoughPearls = len;
    
    /*这里求的是没有绕过环的最小子串长度*/
    for ( int i=0; i<len; ++i) 
    {
        if (!colors[arr[i]])
            colorsCounter++;
        colors[arr[i]]++;

        if (colorsCounter==colorsNeed) 
        {
            firstFoundEnoughPearls = i;
         
            int j = 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;
        }
    }
    
     /*这里求的是绕过尾部的子串,然后到当前开始地方的递归*/
    for (int i=0; i<firstFoundEnoughPearls; ++i) 
    {
        if (!colors[arr[i]])
            colorsCounter++;
        colors[arr[i]]++;
        int j = lastStartIndex;
        while (colors[arr[j]]>1) 
		{
            colors[arr[j]]--;
            ++j;
        }
        if (i-j+1+len<bestLen) 
		{
            bestStartIndex = j;
            bestLen = i-j+1+len;
            if (bestLen==colorsNeed)
                break;
        }
        lastStartIndex = j;
    }

    int offset = bestStartIndex;
    cout<<"bestStartIndex = "<<bestStartIndex<<endl; 
    for (int i=0; i<bestLen;) 
	{
        cout << arr[i+offset] << " ";
        	++i;
        if (i+offset>=len)
            offset = 0-i;
    }
    cout << endl;
}

int main()
{
    int arr[] = {1,2,3,3,2,1,4,1,2,3,2,6,4,5,2,6,2,3,4,1,2,5,2,3,4,5,6};
    int m = sizeof(arr)/sizeof(arr[0]);
    int n = 6;
    find(arr,m,n);

    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐