A robot consisting of a mechanical arm can take a box from the left stack and place it either in stack 1 or in stack 2. We can program the robot by placing a series of numbers consisting of numbers 1 and 2 only. Each number indicates in which stack the box has to be entered.
If given the program that the robot did, find the original layout of the boxes.
Input Format
*Number N.
*A series of N numbers (1 or 2), the program executed by the robot.
*characters "B" (black box) and "W" (white box), the final form of the original stack.
*characters "B" and "W", the final form of the stack 1.
*characters "B" and "W", the final form of the stack 2.
Constraints
Imagine you are doing this with pen and paper, write down how you would go about completing this task - what steps would you take? This is called pseudo code and can be very helpful in organizing ones thoughts and the program structure. Start out very general, then go back and refine each part until you are happy to convert to real code.
Edit:
I would interpret that assignment as being able to use something else apart from a stack to store the 1's and 2's . If you put them in a stack, you won't be able to access the stack from the bottom. So try a std::vector like I suggested earlier. Use the stacks for the B and W though.
Good Luck !!
I am off to bed soon, but Repeater is here, so you are in good hands ! Others too :+)
of course it ends. it ends when there is no more input.
you may observe it by using stream redirection or by sending EOF.
what makes little sense is to keep trying to read after that. (loops 25--27 and 28--30)
however, by looking at your input example, you need to read till end-of-line. use `std::getline()' with a `std::string' or with cin.get() or the noskipws flag and compare against '\0'
int n;
std::cin>>n;
std::vector<int> operations(n);
for(int K=0; K<n; ++K)
std::cin>>operations[K];
std::cin.ignore(); //to discard the '\n' at the end
string original, stack1, stack2;
std::getline(std::cin, original);
std::getline(std::cin, stack1);
std::getline(std::cin, stack2);
//alternative without std::string
char box;
std::cin>>std::noskipws;
while(std::cin>>box and box not_eq '\n')
F.push(box);
while(std::cin>>box and box not_eq '\n')
F1.push(box);
while(std::cin>>box and box not_eq '\n')
F2.push(box);
by the way, i don't understand your example.
the original layout of stack 2 was `WB', ¿how did it become `BWBWW'? you can only add at one end, so it should start or end with WB, but that is not the case.
Thanks a lot,to you too,ne555,you helped me with solving the problem a lot!
by the way, i don't understand your example.
the original layout of stack 2 was `WB', ¿how did it become `BWBWW'? you can only add at one end, so it should start or end with WB, but that is not the case.
It's difficult to understand the exercise because is from an olympiad:0 :)
But i will explain,i did the exercise with stacks and then i had the same problem with the example (i was confused) cause the program wants to print it how it is not in the correct order!