0

i've come across a problem.

How do i check if an array has two,or more elements in a sequence.

For example,let's say i have an array

1,2,3,6,7,8,4,5

and i want to check if it has numbers 6,7,8 but in that sequence.

For example,if it would be

1,2,3,7,6,8,4,5

it would return false.

I know that it's pretty easy with one element,just make a for loop,but i can't figure out how to search for two or more arrays,and in the sequence i want them to be.

6
  • 1
    Where exactly do you run into problems? When trying what? Commented Jan 26, 2013 at 12:08
  • @MrLister well,i haven't really run into them,yet.I haven't tried anything because i don't know what to try. Commented Jan 26, 2013 at 12:09
  • 1
    Well here is something to try: iterate over the array until you find the first number of your sequence. If so, check whether the rest of the sequence is present. If so, you found it! If not, continue searching... Commented Jan 26, 2013 at 12:15
  • Sounds like a simple version of how regex works.. Commented Jan 26, 2013 at 12:15
  • 1
    Nah... I just pointed you to a possible direction to solve this problem. No need to create an full-blown answer for it. Good luck with solving the problem! If you fail at a particular part of the solution, create a new (and less broad) question for it. Commented Jan 26, 2013 at 12:23

2 Answers 2

5

There's an algorithm for that: std::search. Use it and don't care (only care if you want have something sophisticated that is faster than O(n·m)).

// will be superfluous in C++11
template <typename T, std::size_t N> T *begin(T (&arr)[N]) { return arr; }
template <typename T, std::size_t N> T *end  (T (&arr)[N]) { return &arr[N]; }

int main()
{
  int array[] = {1,2,3,6,7,8,4,5};
  int check[] = {6,7,8};

  int *position = std::search(begin(array), end(array), begin(check), end(check));
  if (position != end(array))
    std::cout << "found at position " << position - array << '\n';
  else
    std::cout << "not found\n";
}
Sign up to request clarification or add additional context in comments.

Comments

0

This is should yield O(n):
(edit: but it works correctly only with sequences that don't contain cycles)

bool has_a_sequence(const std::vector<int>& where, const std::vector<int>& seq_no_cycles)
{
    if(!seq_no_cycles.size())
    {
        return false;
    }
    std::vector<int>::const_iterator where_iter;
    std::vector<int>::const_iterator seq_iter = seq_no_cycles.begin();    
    for(where_iter = where.begin(); where_iter != where.end(); where_iter++)
    {
        if(*where_iter == *seq_iter)
        {
            seq_iter++;
            if(seq_iter == seq_no_cycles.end())
            {
                break;
            }
        }
        else
        {
            seq_iter = seq_no_cycles.begin();
            if(*where_iter == *seq_iter)
            {
                seq_iter++;
            }
        }
    }
    return seq_iter == seq_no_cycles.end();
}

5 Comments

It's O(n) but it's wrong in the general case where sequence can contain duplicate elements.
is it? could you give an example?
..as I couldn't find one - it seems to work correctly wit sequences having duplicated elements - try it out yourself, or am I missing something?
Yeah, you're correct thanks ipc!. This code indeed doesn't work with some sequences that have cycles. I'm laughing now - it was kinda naive to try with O(n) - is there really anything better than O(mn)? ;) BTW: should posts with code that's wrong be removed - not to leave spam or should I leave it here? Thanks :)
There is KMP, that works in O(n+m) and it's variants (like Boyer–Moore), that also work in O(n+m). But they are nasty to code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.