120-stacks of Flapjacks
Time limit:3.000 seconds
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=98&page=show_problem &problem=56
Background
Stacks and queues are often considered the bread and butter of data structures and find use in architecture, parsing, oper Ating systems, and discrete event simulation. Stacks are also important in the theory of formal.
This problem involves both butter and sustenance into the form of pancakes rather than bread in addition to a finicky server Who flips pancakes according to a unique, but complete set of rules.
The Problem
Given a stack of pancakes, you are to write a program that indicates how the stack can is sorted so this largest panca Ke is 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 have different diameters.
Sorting a stack is do by a sequence of pancake ' flips '. A flip consists of inserting a spatula between two pancakes in a stack and flipping (reversing) the pancakes on the Spatul A (reversing the sub-stack). A The flip is specified by giving the position of the pancake on the bottom of the sub-stack to be flipped (relative to the WH OLE Stack). The pancake on the bottom of the whole stack has position 1 and the pancake in the top of a stack of n pancakes has Positi On N.
A stack is specified by giving the diameter of the all pancake in the stack in which the pancakes.
For example, consider the three stacks of pancakes below (in which pancake 8 are the top-most pancake of the left stack):
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).
The Input
The input consists of a sequence of stacks of pancakes. Each stack would consist of between 1 and pancakes and each pancake would have an integer diameter between 1 and 100. The input is terminated by End-of-file. Each stack are given as a single line of input and the top pancake on a stack appearing a line, the bottom Pancak E appearing last, and all pancakes separated by a.
The Output
For each stack of pancakes, the output should echo-original stack on one line, followed by some sequence of flips of Results in the stack of pancakes being sorted so this 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 and no more flips should to be made.
Sample Input
1 2 3 4, 5 5 4 3 2 1 5 1 2 3 4
Sample Output
1 2 3 4 5 0 5 4 3 2 1 1 0 5 1 2 3 4 1 2-
0
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/
Consider a variety of reading methods, and ultimately feel Istringstream ISS (s);
Details of the operation of the code.
Complete code:
/*0.019s*/#include <cstdio> #include <sstream> #include <algorithm> #include <deque> us
ing namespace std;
Char s[100];
Deque<int> D;
Deque<int>::iterator ITER, MAXN;
int main () {int A;
while (gets (s)) {puts (s);
Istringstream ISS (s);
D.clear ();
While (ISS >> a) d.push_front (a);
for (iter = D.begin (); Iter!= d.end (); ++iter) {MAXN = Max_element (iter, D.end ());
if (MAXN!= iter)///indicates that the current bit is not the expected element, rollover operation {if (MAXN!= d.end ()-1)///maximum is not a top element, you must flip it to top
{Reverse (MAXN, d.end ());
printf ("%d", Maxn-d.begin () + 1);
Reverse (ITER, d.end ());///flips the current bit to printf ("%d", Iter-d.begin () + 1);
} puts ("0");
return 0; }