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

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

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

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

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

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

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

          如果不是首尾相连的:

[cpp]  view plain copy
  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4. #define MAXN 10  
  5. int colors[MAXN];//record the counter of one color  
  6. int colorsCounter;  
  7. void find(int arr[],int len, int colorsNeed)  
  8. {  
  9.     int bestStartIndex = 0;  
  10.     int bestLen = len;  
  11.     int lastStartIndex = 0;  
  12.       
  13.     for ( int i=0; i<len; ++i)   
  14.     {  
  15.         if (!colors[arr[i]])//如果当前数字还没有被记录,则说明又找到一个新的   
  16.             colorsCounter++;  
  17.         colors[arr[i]]++;  
  18.   
  19.         if (colorsCounter==colorsNeed) //需要的个数已经查找够了   
  20.         {  
  21.             int j = lastStartIndex;  
  22.             while (colors[arr[j]]>1) //直到当前 元素arr 在以后的子序列中不再出现   
  23.             {  
  24.                 colors[arr[j]]--;  
  25.                 ++j; //查找下一个arr[]颜色,看是否以后还存在   
  26.             }  
  27.               
  28.             if (i-j+1<bestLen) //当前子序列小于当前的最佳长度   
  29.             {  
  30.                 bestStartIndex = j;//记录开始位置   
  31.                 bestLen = i-j+1;   //记录最佳长度   
  32.                 if (bestLen==colorsNeed)//如果正好等于需要颜色数,则说明每一个正好一个。则退出   
  33.                     break;  
  34.             }  
  35.             lastStartIndex = j;//记录本次开始的位置   
  36.         }  
  37.     }  
  38.   
  39.     cout << "bestStartIndex: " <<bestStartIndex<< endl;  
  40.     cout << "bestLen: " <<bestLen<< endl;  
  41.     for (int i=bestStartIndex; i<bestLen+bestStartIndex; ++i)  
  42.         cout << arr[i] << " ";  
  43.     cout << endl;  
  44. }  
  45.   
  46. int main()  
  47. {  
  48.     int arr[] = {1,2,3,3,2,1,4,1,3,4,5,5,6,2,3,4,4,1,5,2,3,4};  
  49.     int m = sizeof(arr)/sizeof(arr[0]);  
  50.   
  51.     for ( int i=0; i<m; ++i)  
  52.         cout << arr[i] <<" ";  
  53.     cout << endl;  
  54.   
  55.     int n = 6;  
  56.     find(arr,m,n);  
  57.   
  58.     return 0;  
  59. }  


 

如果带环的话:

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

[cpp]  view plain copy
  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4. #define MAXN 10  
  5. int colors[MAXN];//record the counter of one color  
  6. int colorsCounter;  
  7. void find(int arr[],int len, int colorsNeed)  
  8. {  
  9.     int bestStartIndex = 0;  
  10.     int bestLen = len;  
  11.     int lastStartIndex = 0;  
  12.     int firstFoundEnoughPearls = len;  
  13.       
  14.     /*这里求的是没有绕过环的最小子串长度*/  
  15.     for ( int i=0; i<len; ++i)   
  16.     {  
  17.         if (!colors[arr[i]])  
  18.             colorsCounter++;  
  19.         colors[arr[i]]++;  
  20.   
  21.         if (colorsCounter==colorsNeed)   
  22.         {  
  23.             firstFoundEnoughPearls = i;  
  24.            
  25.             int j = lastStartIndex;  
  26.             while (colors[arr[j]]>1)   
  27.             {  
  28.                 colors[arr[j]]--;  
  29.                 ++j;  
  30.             }  
  31.             if (i-j+1<bestLen)  
  32.             {  
  33.                 bestStartIndex = j;  
  34.                 bestLen = i-j+1;  
  35.                 if (bestLen==colorsNeed)  
  36.                     break;  
  37.             }  
  38.             lastStartIndex = j;  
  39.         }  
  40.     }  
  41.       
  42.      /*这里求的是绕过尾部的子串,然后到当前开始地方的递归*/  
  43.     for (int i=0; i<firstFoundEnoughPearls; ++i)   
  44.     {  
  45.         if (!colors[arr[i]])  
  46.             colorsCounter++;  
  47.         colors[arr[i]]++;  
  48.         int j = lastStartIndex;  
  49.         while (colors[arr[j]]>1)   
  50.         {  
  51.             colors[arr[j]]--;  
  52.             ++j;  
  53.         }  
  54.         if (i-j+1+len<bestLen)   
  55.         {  
  56.             bestStartIndex = j;  
  57.             bestLen = i-j+1+len;  
  58.             if (bestLen==colorsNeed)  
  59.                 break;  
  60.         }  
  61.         lastStartIndex = j;  
  62.     }  
  63.   
  64.     int offset = bestStartIndex;  
  65.     cout<<"bestStartIndex = "<<bestStartIndex<<endl;   
  66.     for (int i=0; i<bestLen;)   
  67.     {  
  68.         cout << arr[i+offset] << " ";  
  69.             ++i;  
  70.         if (i+offset>=len)  
  71.             offset = 0-i;  
  72.     }  
  73.     cout << endl;  
  74. }  
  75.   
  76. int main()  
  77. {  
  78.     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};  
  79.     int m = sizeof(arr)/sizeof(arr[0]);  
  80.     int n = 6;  
  81.     find(arr,m,n);  
  82.   
  83.     return 0;  
  84. }  
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值