简体   繁体   中英

Extract a certain sequence of numbers from a vector

I am trying to find a way to extract from a series of numbers the longest sequence that meets the condition :each number must be the prefix of the number that follows it.

Ex: For the series : 523,742,7421,12,123,1234,87 it should display 12,123,1234 .

I thought about storing the series in a vector and then iterate it and move the numbers that meet the condition in another vector. However, I got stuck at choosing the longest sequence ( 12,123,1234 instead of 742, 7421 in the above example).Here is the code I wrote so far:

       bool prefix(int a, int b){
           if ((b / 10 - ((b % 10)) / 10) == a)
           return 1;
           else return 0;
          }

       vector<int> choose_sequence(vector<int> &series){
          vector<int> right_sequence;
           int count = 0;
              for (int i = 0; i < series.size();){
                for (int j = i + 1; j < series.size();){
                   if (prefix(series.at(i), series.at(j))){
                  right_sequence.push_back(series.at(i));
                  right_sequence.push_back(series.at(j));
                        i=j;
                        j++;

                   }
                 else 
                 i++;
            }
              }
          return right_sequence;
       }

Any suggestion or correction is welcomed and most appreciate.Also,if you know a better way to do this using another data type than vectors,please share.

I think the classic way of solving this would be to create a second vector that will contain, for each position, the length of the sequence that ends at that position and the position of the previous element in the sequence (-1 if there is no previous element). For your example, the vectors would be something like: count: 1, 1, 2, 1, 2, 3, 1 prev: -1, -1, 1, -1, 3, 4, -1

You will chose the maximum value from count and determine the sequence using prev.

So, our maximum is 3, the position is 5 and the corresponding element is 1234. The previous element is at position 4 in the original vector and the value is 123. The previous element is at position 3 and the value is 12. The previous element is at position -1 which means there is no previous element so we have our sequence: 12, 123, 1234.

Another approach would be trying to set the length of the sequence that starts at a certain position and the position of the next element. You would have: count: 1, 2, 1, 3, 2, 1, 1 next: -1, 2, -1, 4, 5, -1, -1

Our maximum is again 3, but now we have the first element of the sequence (position 3, value 12). The next element is on position 4 (the value is 123). The next element is on position 5 (the value is 1234). The next element is in position -1 which means we reached the end of the sequence.

The advantage of this approach is that the sequence is generated in the "correct" order.

You can do this without any additional memory and only for one traversal of the input vector.

std::vector<int> longestSequence(const std::vector<int>& numbers)
{
    std::vector<int> result;
    if (numbers.empty())
        return result;
    size_t longestStart = 0, longestLength = 0;
    size_t start = 0;
    for (size_t i = 1, imax = numbers.size(); i < imax; ++i) {
        if (numbers[i] / 10 != numbers[i - 1]) {
            if (i - start > longestLength) {
                longestStart = start;
                longestLength = i - start;
            }
            start = i;
        }
    }
    if (numbers.size() - start > longestLength) {
        longestStart = start;
        longestLength = numbers.size() - start;
    }
    result.assign(begin(numbers) + longestStart, begin(numbers) + longestStart + longestLength);
    return result;
}

link to Ideone

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM