Uva120-stacks of Flapjacks (STL)

Source: Internet
Author: User


Background
background


Stacks and Queues is often considered the bread and butter of data structures and find use in architecture, parsing, oper Ating systems, and discrete event simulation. Stacks is also important in the theory of formal languages.
Stacks and queues are often considered as bread and butter in data structures, and are widely used in architecture, analysis, operating systems, and discrete events. The stack also plays an important role in the theory of formal language.



This problem involves both butter and sustenance in the form of pancakes rather than bread in addition to a finicky server Who flips pancakes according to a unique and complete set of rules.
The problem is for a picky chef to flip pancakes in a unique and complete set of rules to keep the butter and nutrients from being burnt out in pancakes (not bread). (This sentence really do not know how to translate good, but also hope that the teachers correct me!) Ps. This problem is more difficult to translate than to solve the problem of the most!! )





The problem
problem


Gi Ven a stack of pancakes, you is to write a program This indicates how the stack can is sorted so that the largest pancake are on the bottom and the smallest pancake are on the top. The size of a pancake is given by the pancake ' s diameter. All pancakes in a stack has different diameters.
Given a stack of pancakes, you're going to write a program to figure out how to make this stack of pancakes from the bottom up from the top up to the small arrangement. Given the size of a pancake's radius, a stack of pancakes varies in size.



So Rting a stack is do by a sequence of pancake "flips". A flip consists of inserting a spatula between, pancakes in a stack and flipping (reversing) the pancakes on the Spatul A (reversing the sub-stack). A Flip is specified by giving the position of the pancake on the bottom of the sub-stack to being flipped (relative to the WH OLE Stack). The pancake on the bottom of the whole stack have position 1 and the pancake on the top of a stack of n pancakes have Positi On N.
give



A stack is specified by giving the diameter of each pancake in the "the" order in which the pancakes appear.
a a pancake A stack consists of a group of numbers that represent the diameter of each pancake, and the order in which they are arranged is the order of the numbers given.



For example, consider the three stacks of pancakes below (in which pancake 8 are the top-most pancake of the left stack):
Like the following three pancake stacks (pancake 8 is the top of the stack on the left)


8 7 2
4 6 5
6 4 8
7 8 4
5 5 6
2 2 7


The stack on the left can is transformed to the stack in the middle via Flip (3). The middle stack can be transformed to the right stack via the command Flip (1).
a stack on the left can be by flipping the 3rd pancake into the middle of a stack of orders. The middle stack can be reversed by flipping the 1th pancake into a stacked order on the right.





The Input
input


The input consists of sequence of stacks of pancakes. Each stack would consist of between 1 and pancakes and each pancake would has an integer diameter between 1 and 100. The input is terminated by End-of-file. Each stack was given as a single line of input with the top pancake on a stack appearing first on a line, the bottom Pancak E appearing last, and all pancakes separated by a space.
The input includes a series of pancake stacks. Each stack consists of 1 to 30 pancakes, and each pancake has a diameter of 1 to 100. The input ends with EOF. Each stack of pancake exclusive row, the top of the first row, the bottom of the line at the end, the middle of each pancake separated by a space.





The Output
Output


For each stack of pancakes, the output should echoes the original stack on one line, followed by some sequence of flips Results in the stacks of pancakes being sorted so, the largest diameter pancake are on the bottom and the smallest on t Op. For each stack the sequence of flips should is terminated by a 0 (indicating no more flips necessary). Once a stack is sorted, no more flips should be made.
corresponding to each stack of pancake data, the original stack must be output in the first row, followed by the output of a set of flip action sequences, so that the stack of pancakes from the bottom up from the top to the small arrangement. Each set of rollover action sequences for the output is terminated by the (means no longer flipped). Once a stack of pancakes has been sequenced, no more flipping can be done.





Sample Input
Input Example


1 2 3) 4 5
5 4 3) 2 1
5 1 2) 3 4





Sample Output
Sample Output


1 2 3) 4 5
0
5 4 3) 2 1
1 0
5 1 2) 3 4
1 2 0





Analysis
Analysis


The algorithm is very simple, give you a group of pancakes, with a pen on the paper to know how to do a picture. Or the idea of dynamic planning, from the bottom to the top, to keep the pancakes have been all over the example is the largest and orderly. For example, the input data is:



2 4 1) 3 5



According to the topic requirement, 4 at the top 5 at the bottom. 5 was already the largest, then moved to the last pancake 3. 3 above (inclusive) the largest is 4, first turn 4 to the top, forming:



4 2 1) 3 5



Then flip the 4 to 3 sub-stack to form:



3 1 2) 4 5



Move to the top of the previous pancake 2,2 (inclusive) The largest is 3, while 3 is on the tops, so direct 2 to 3 flips, forming:



2 1 3) 4 5



Finally, 2 and 1 are flipped and finished. Note: Be sure not to forget to copy the original data to the output line under the input line of data and omit the inevitable WA. With the algorithm above, there is no extra rollover, so don't worry.








#include <algorithm>
#include <iostream>
#include <iterator>
#include <deque>
#include <string>
#include <sstream>
using namespace std;
/ / main function
int main(void) {
//Loop through each set of input strings. Output the last 0 and line feed one round at a time
for (string strLine; getline(cin, strLine); cout << '0' << endl) {
//Respond to input character serial as required
cout << strLine << endl;
//Construct character stream, convert from pass to number
istringstream iss(strLine);
//Convert the string to a number, and store the reverse order (the bottom is the first) in stack
deque<int> Stack;
for (int nDiam; iss >> nDiam; Stack.push_front(nDiam));
//Turn it up from the bottom, keeping the top of I smaller than I
for (deque<int>::iterator i = Stack.begin(); i != Stack.end(); ++i) {
//Find the largest element above (including) I
deque<int>::iterator iMax = max_element(i, Stack.end());
//Continue if the largest element is I (point I to the previous one)
If (IMAX! = I) {/ / otherwise, you need to flip it
//If the largest one is not at the top, you need to turn it to the top first
if (iMax != Stack.end() - 1) {
reverse(iMax, Stack.end());
//Output flip start
cout << distance(Stack.begin(), iMax) + 1 << ' ';
}
//Flip the largest from the top to the I position
reverse(i, Stack.end());
//Output flip start
cout << distance(Stack.begin(), i) + 1 << ' ';
}
}
}
Return 0;
}






Uva120-stacks of Flapjacks (STL)


Contact Us

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.