基本思路就是DFS回溯
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;
public class ZOJ1004
{
static Stack<Character> stack = new Stack<Character>();
static List<String> result = new ArrayList<String>();
static void dfs(String io, String from, String to)
{
if (to.isEmpty())
{
result.add(io);
return;
}
if (!stack.isEmpty() && stack.peek() == to.charAt(0))
{
stack.pop();
dfs(io + 'o', from, to.substring(1));
stack.push(to.charAt(0));
}
if (!from.isEmpty())
{
stack.push(from.charAt(0));
dfs(io + 'i', from.substring(1), to);
stack.pop();
}
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
while (sc.hasNext())
{
String s1 = sc.nextLine();
String s2 = sc.nextLine();
result.clear();
dfs("", s1, s2);
Collections.sort(result);
System.out.println("[");
for (String s : result)
{
char[] chs = s.toCharArray();
for (int i = 0; i < chs.length; i++)
System.out.print(chs[i] + " ");
System.out.println();
}
System.out.println("]");
}
sc.close();
}
}