UVa732 - Anagrams by Stack(DFS)

How can anagrams result from sequences of stack operations? There are twosequences 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. Yourprogram should, given pairs of words produce sequences of stackoperations which convert the first word to the second.

Input 

The input will consist of several lines of input. The first lineof each pair of input lines is to be considered as a source word(which does not include the end-of-line character). The secondline (again, not including the end-of-line character) of each pairis a target word.

Output 

For each input pair, your program should produce a sorted list ofvalid sequences of i and o which produce the targetword from the source word. Each list should be delimited by

[
]
and the sequences should be printed in ``dictionary order". Withineach sequence, each i and o is followed by a singlespace and each sequence is terminated by a new line.

Process 

A stack is a data storage and retrieval structure permitting twooperations:


Push - to insert an item and

Pop - to retrieve the most recently pushed item

We will use the symbol i (in) for push and o (out) forpop operations for an initially empty stack of characters. Givenan input word, some sequences of push and pop operations are validin that every character of the word is both pushed and popped, andfurthermore, no attempt is ever made to pop the empty stack. Forexample, 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 inputword. For example, the input word FOO and the sequence i i oi o o produce the anagram OOF. So also would the sequence ii i o o o. You are to write a program to input pairs of wordsand output all the valid sequences of i and o whichwill 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
]
在搜索时,分两种情况:

1、直接进栈

2、当栈顶元素与目标元素一样时,出栈

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.ArrayList;

public class Main implements Runnable
{
	private static final boolean DEBUG = false;
	private PrintWriter cout;
	private Scanner cin;
	private String src, dst;
	
	private void init() 
	{
		try {
			if (DEBUG) {
				cin = new Scanner(new BufferedInputStream(new FileInputStream(
						"d:\\OJ\\uva_in.txt")));
			} else {
				cin = new Scanner(new BufferedInputStream(System.in));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		cout = new PrintWriter(new OutputStreamWriter(System.out));
	}

	private boolean input()
	{
		if (!cin.hasNext()) return false;
		
		src = cin.next();
		dst = cin.next();
		
		return true;
	}
	
	private void dfs(ArrayList<Character> src, ArrayList<Character> dst, ArrayList<Character> ans, ArrayList<Character> stack)
	{
		if (!src.isEmpty()) {
			ans.add('i');
			stack.add(src.get(src.size() - 1));
			src.remove(src.size() - 1);
			dfs(src, dst, ans, stack);
			src.add(stack.get(stack.size() - 1));
			stack.remove(stack.size() - 1);
			ans.remove(ans.size() - 1);
		}
		
		if (!stack.isEmpty() && stack.get(stack.size() - 1) == dst.get(stack.size() + src.size() - 1)) {
			ans.add('o');
			char ch = stack.get(stack.size() - 1);
			stack.remove(stack.size() - 1);
			dfs(src, dst, ans, stack);
			stack.add(ch);
			ans.remove(ans.size() - 1);
		}
		
		if (src.isEmpty() && stack.isEmpty()) {
			for (int i = 0; i < ans.size(); i++) {
				if (i != 0) cout.print(" ");
				cout.print(ans.get(i));
			}
			cout.println();
		}
	}
	
	private void solve()
	{
		cout.println("[");
		
		if (src.length() == dst.length()) {
			ArrayList<Character> srcArr = new ArrayList<Character>();
			for (int i = src.length() - 1; i >= 0; i--) {
				srcArr.add(src.charAt(i));
			}
		
			ArrayList<Character> dstArr = new ArrayList<Character>();
			for (int i = dst.length() - 1; i >= 0; i--) {
				dstArr.add(dst.charAt(i));
			}
			
			ArrayList<Character> ans = new ArrayList<Character>();
			ArrayList<Character> stack = new ArrayList<Character>();
			
			dfs(srcArr, dstArr, ans, stack);
		}
		
		cout.println("]");
		cout.flush();
	}
	
	public void run()
	{
		init();
		
		while (input()) {
			solve();
		}
	}
	
	public static void main(String[] args) 
	{
		new Thread(new Main()).start();
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值