ZOJ1004
How can anagrams result from sequences of stack operations? There are two sequences of stack operators which can convert TROT to TORT:
颠倒字母顺序构成的词如何产生于栈操作顺序?两种栈操作顺序可以将TROT转换成TORT:
[ i i i i o o o o i o i i o o i o ]
where i stands for Push and o stands for Pop. Your program should, given pairs of words produce sequences of stack operations which convert the first word to the second.
i代表入栈,o代表出栈。你的程序可以将一对词语产生栈操作顺序,它将第一个词转换成第二个词。
The input will consist of several lines of input. The first line of each pair of input lines is to be considered as a source word (which does not include the end-of-line character). The second line (again, not including the end-of-line character) of each pair is a target word. The end of input is marked by end of file.
输入包括几行,第1行是一个源词语(它不包含行尾字符)。第2行(它也不包含行尾字符)是目标词语。输入结束是用文件结束标记标识。
Output
For each input pair, your program should produce a sorted list of valid sequences of i and o which produce the target word from the source word. Each list should be delimited by
每一对输入,你的程序应该生成一个排序列表,包含有效的i和o的顺序,这将可以生成目标词语。每一个列表应该被[]限制。
[ ]
and the sequences should be printed in "dictionary order". Within each sequence, each i and o is followed by a single space and each sequence is terminated by a new line.
顺序应该按照字典序列。在每一个顺序中,i和o由一个空格,由换行符中断一行。
Process
A stack is a data storage and retrieval structure permitting two operations:
一个栈是一个数据存储和获取的结构允许两种操作
We will use the symbol i (in) for push and o (out) for pop operations for an initially empty stack of characters. Given an input word, some sequences of push and pop operations are valid in that every character of the word is both pushed and popped, and furthermore, no attempt is ever made to pop the empty stack. For example, if the word FOO is input, then the sequence:
我们将使用i表示入栈,o标识出栈。假如输入一个词语,
| i i o i o o | is valid, but |
| i i o | is not (it's too short), neither is |
| i i o o o i | (there's an illegal pop of an empty stack) |
Valid sequences yield rearrangements of the letters in an input word. For example, the input word FOO and the sequence i i o i o o produce the anagram OOF. So also would the sequence i i i o o o. You are to write a program to input pairs of words and output all the valid sequences of i and o which will produce the second member of each pair from the first.
Sample Input
madam adamm bahama bahama long short eric rice
Sample Output
[ i i i i o o o i o o i i i i o o o o i o i i o i o i o i o o i i o i o i o o i o ] [ i o i i i o o i i o o o i o i i i o o o i o i o i o i o i o i i i o o o i o i o i o i o i o i o ] [ ] [ i i o i o i o o ]

浙公网安备 33010602011771号