# A string of beads (m) connected to the end, with N colors (n<=10), so that the shortest string contains all colors

Source: Internet
Author: User

idea: First search from index=0, each check a bead, response color number +1, if the new color is the total color number +1.

When the total number of colors is n, the first continuous sequence that satisfies the condition is found.

1> Search from the beginning of the sequence, if the number of colors in the search is not 1, it indicates that the string has other beads have the color, continue to search and update the sequence, starting at index position +1.

If the number of colors at the search is 1, stop the search.

2> Compare the best sequence length with the current sequence long, updating the best sequence. Records the starting position of the current sequence.

Continue index++ from the first sequence that satisfies the condition, and check the conditions.

If it is not connected to the end:

`#include <iostream>using namespace std; #define MAXN 10int Colors[maxn];//record The counter of one colorint Colorsco    Unter;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])//If the current number is not recorded, then a new colorscounter++ is found;        colors[arr[i]]++;            if (colorscounter==colorsneed)//required number has been found enough {int j = Laststartindex;                while (colors[arr[j]]>1)//until the current element arr no longer appears in the subsequent subsequence {colors[arr[j]]--; ++j;                Find Next arr[] color to see if there is a later} if (I-j+1<bestlen)//The current sub-sequence is less than the current optimal length {   Beststartindex = j;//record start position bestlen = i-j+1; Record the best length if (bestlen==colorsneed)//if exactly equal to the number of colors required, it means that each one is exactly one.            Exit the break; } laststartindex = j;//record the location of the start}} 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);    for (int i=0; i<m; ++i) cout << arr[i] << "";    cout << Endl;    int n = 6;    Find (Arr,m,n); return 0;}`

If band Ring:

add another loop, starting with the last time you get the smallest length, to the beginning, where all the elements are met for the first time.

`#include <iostream>using namespace std; #define MAXN 10int Colors[maxn];//record The counter of one colorint Colorsco    Unter;void find (int arr[],int len, int colorsneed) {int beststartindex = 0;    int bestlen = len;    int laststartindex = 0;        int firstfoundenoughpearls = len;        /* This is for the longest string length without bypassing the ring */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;       }}/* Here is the substring that bypasses the tail, then the recursion at the beginning of the current */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);    int n = 6;    Find (Arr,m,n); return 0;}`

A string of beads (m) connected to the end, with N colors (n<=10), so that the shortest string contains all colors

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.