mardi 2 juin 2015

Finding repeated state of the Queue (same elements in order) in C++

While solving some problem using C++ I used inbuilt Queue template to create a Queue. Now I need to check the state of the Queue after each operation on the Queue and report if the Queue happens to contain same elements (in same order) as in ANY one of the previous states of the Queue. For example, consider the following step-wise operations on the Queue (Left-hand-side represents the front and Right-hand-side represents rear/back of Queue):

  1. (2, 4, 5, 1)
  2. (4, 5, 1)
  3. (4, 5, 1, 3)
  4. (4, 5, 1, 3, 2)
  5. (4, 5, 1, 3, 2, 4)
  6. (4, 5, 1, 3, 2, 4, 5)
  7. (5, 1, 3, 2, 4, 5)
  8. (5, 1, 3, 2, 4, 5, 1)
  9. (1, 3, 2, 4, 5, 1)
  10. (3, 2, 4, 5, 1)
  11. (2, 4, 5, 1)

So the state of the Queue at step-11 is same (elements as well as order of elements) as in step-1. I need to find when this happens using C++. I was thinking of using Map to store previous states of Queue, but cannot figure out exactly how to do this?

Aucun commentaire:

Enregistrer un commentaire