{"trustable":false,"sections":[{"title":"","value":{"format":"PLAIN","content":"Stacks and Queues are often considered the bread and butter of data structures and find use in architecture, parsing, operating systems, and discrete event simulation. Stacks are also important in the theory of formal languages.\n\nThis problem involves both butter and sustenance in the form of pancakes rather than bread inaddition to a finicky server who flips pancakes according to a unique, but complete set of rules.\n\nGiven a stack of pancakes, you are to write a program that indicates how the stack can be sorted so that the largest pancake is on the bottom and the smallest pancake is on the top. The size of apancake is given by the pancake’s diameter. All pancakes in a stack have different diameters.\n\nSorting a stack is done by a sequence of pancake “flips”. A flip consists of inserting a spatula between two pancakes in a stack and flipping (reversing) all the pancakes on the spatula (reversing the sub-stack). A flip is specified by giving the position of the pancake on the bottom of the sub-stack tobe flipped (relative to the whole stack). The pancake on the bottom of the whole stack has position 1 and the pancake on the top of a stack of n pancakes has positionn.A stack is specified by giving the diameter of each pancake in the stack in the order in which the pancakes appear.\n\nFor example, consider the three stacks of pancakes below (in which pancake 8 is the top-most pancake of the left stack):872465648784556227\n\nThe stack on the left can be transformed to the stack in the middle via flip(3). The middle stack canbe transformed into the right stack via the command flip(1).\n\nInput\n\n\nThe input consists of a sequence of stacks of pancakes. Each stack will consist of between 1 and 30 pancakes and each pancake will have an integer diameter between 1 and 100. The input is terminated by end-of-file. Each stack is given as a single line of input with the top pancake on a stack appearing first on a line, the bottom pancake appearing last, and all pancakes separated by a space.\n\nOutput\nFor each stack of pancakes, the output should echo the original stack on one line, followed by some sequence of flips that results in the stack of pancakes being sorted so that the largest diameter pancake is on the bottom and the smallest on top. For each stack the sequence of flips should be terminated by a ‘0’ (indicating no more flips necessary). Once a stack is sorted, no more flips should be made.\n\nSample Input\n\n1 2 3 4 5\n5 4 3 2 1\n5 1 2 3 4\n\nSample Output\n\n1 2 3 4 5\n0\n5 4 3 2 1\n1 0\n5 1 2 3 4\n1 2 0"}},{"title":"","value":{"format":"PLAIN","content":"栈和队列通常被认为是数据结构的面包和黄油,在架构、解析、操作系统和离散事件模拟中都有使用。栈在形式语言的理论中也很重要。\n\n这个问题涉及到黄油和养料的形式,即煎饼而不是面包,此外还有一个精巧的服务器,他根据一套独特但完整的规则翻转煎饼。\n\n给定一叠煎饼,你要写一个程序,说明如何对这叠煎饼进行排序,使最大的煎饼在下面,最小的煎饼在上面。煎饼的大小由煎饼的直径给出。一堆煎饼中的所有煎饼都有不同的直径。\n\n通过一系列的煎饼 \"翻转 \"来对一堆煎饼进行分类。一个翻转包括在一个堆栈中的两个煎饼之间插入一个铲子,并翻转(反转)铲子上的所有煎饼(反转子堆)。翻转是通过给出要翻转的子堆栈底部的煎饼的位置(相对于整个堆栈)来指定的。整个堆栈底部的煎饼的位置是1,而n个煎饼堆栈顶部的煎饼的位置是n.一个堆栈是通过给出堆栈中每个煎饼的直径,按照煎饼出现的顺序来指定的。\n\n例如,考虑以下三叠煎饼(其中煎饼8是左边一叠煎饼的最上面):872465648784556227。\n\n左边的堆栈可以通过flip(3)转化为中间的堆栈,中间的堆栈可以通过命令flip(1)转化为右边的堆栈。中间的堆栈可以通过命令flip(1)转换为右边的堆栈。\n\n输入内容\n\n\n输入由一叠煎饼的序列组成,每叠煎饼由1到30个煎饼组成,每个煎饼的直径在1到100之间。每个堆栈由1到30个煎饼组成,每个煎饼的直径在1到100之间。输入以文件结束结束。每个堆栈以单行输入的形式给出,堆栈中的上层煎饼首先出现在一行,下层煎饼最后出现,所有煎饼之间用空格隔开。\n\n輸出\n对于每一叠煎饼,输出应该在一行上呼应原始堆栈,然后是一些翻转序列,结果是煎饼堆栈被排序,使直径最大的煎饼在底部,最小的在顶部。对于每一个堆栈,翻转序列应该以\u00270\u0027结束(表示不需要再翻转)。一旦一个堆栈被排序,就不应该再进行翻转。\n\n输入示例\n\n1 2 3 4 5\n5 4 3 2 1\n5 1 2 3 4\n\n采样输出\n\n1 2 3 4 5\n0\n5 4 3 2 1\n1 0\n5 1 2 3 4\n1 2 0"}}]}