题目概述:
How can anagrams result from sequences of stack operations? There are two sequences of stack operators which can convert TROT to 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.
Input
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.
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
[ ]
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.
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 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 ]
java实现代码:
import java.util.LinkedList; import java.util.Scanner; import java.util.Stack; public class Main { /** * 代表要求的输入和输出 * @author Administrator * */ public static class Pair { public LinkedList<Character> input; public String target; } public static void main(String[]args) { Scanner scanner = new Scanner(System.in); boolean pairFlag = true; Pair pair = new Pair(); while(scanner.hasNext()) { if(pairFlag) { pair.input = toLinkedList(scanner.nextLine()); pairFlag = !pairFlag; } else { pair.target = scanner.nextLine(); pairFlag = !pairFlag; excute(pair); pair = new Pair(); } } } public static void excute(Pair pair) { System.out.println("["); if(pair.input.size() == pair.target.length()) { //记录压栈与出栈记录 char[] state = new char[pair.input.size()*2]; //记录栈里面元素 Stack<Character> stack = new Stack<Character>(); DFS(pair,stack,state,0,0,pair.input.size()) ; } System.out.println("]"); } /** * * @param pair //输入与目的字符串 * @param stack //栈 * @param state //进栈与出栈记录 * @param nPush //当前已经入栈次数 * @param nPop //当前已经出栈次数 * @param len //字符串长度 */ public static void DFS(Pair pair,Stack<Character> stack,char[] state,int nPush,int nPop,int len) { if(nPush == len && nPop == len) //压栈与出栈次数和字符串一样时,代表已经找到目的字符串,结束搜素 { printState(state); } else { if(nPush < len) //压栈条件 { stack.add(pair.input.remove()); state[nPush + nPop] = 'i'; DFS(pair,stack,state,nPush +1,nPop,len); state[nPush + nPop] = 'n'; pair.input.addFirst(stack.pop()); } if(nPop < nPush && stack.peek() == pair.target.charAt(nPop))//出栈条件-出栈次数少于压栈次数,且出栈的元素是目标字符串索引为(已出栈次数) { pair.input.add(stack.pop()); state[nPush + nPop] = 'o'; DFS(pair,stack,state,nPush,nPop + 1,len); state[nPush + nPop] = 'n'; stack.add(pair.input.removeLast()); } } } private static void printState(char[] state) { for(char c : state) { System.out.print(c + " "); } System.out.println(); } /** * 将输入字符串用链表表示,方便插入与删除 * @param str * @return */ public static LinkedList<Character> toLinkedList(String str) { LinkedList<Character> list = new LinkedList<Character>(); for(int i = 0; i < str.length();i++) { list.add(str.charAt(i)); } return list; } }