package com.supermars.practice;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
/*
* DBACEGF ABCDEFG
BCAD CBAD
*/
public class 二叉树重建 {
public static void main(String[] args) throws FileNotFoundException {
Scanner cin = new Scanner(
new File(
"E:\\jspWorkspace\\programArithmetic\\src\\com\\supermars\\practice\\in.txt"));
while (cin.hasNext()) {
String s1 = cin.next();
String s2 = cin.next();
char[] c1 = s1.toCharArray();
char[] c2 = s2.toCharArray();
// System.out.println(s1+" "+s2);
build(c1, c2, 0, 0, c1.length);
System.out.println("");
}
}
private static void build(char[] c1, char[] c2, int i, int j, int n) {
if (n <= 0)
return;
char pre = c1[i];
int p = 0;
while (p < n && !(pre == c2[j + p])) {// find p inList's Pos
p++;
}
build(c1, c2, i + 1, j, p); // left tree build
build(c1, c2, i + 1 + p, j + p + 1, n - 1 - p); // right tree build
System.out.print(pre+" ");
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
/*
* DBACEGF ABCDEFG
BCAD CBAD
*/
public class 二叉树重建 {
public static void main(String[] args) throws FileNotFoundException {
Scanner cin = new Scanner(
new File(
"E:\\jspWorkspace\\programArithmetic\\src\\com\\supermars\\practice\\in.txt"));
while (cin.hasNext()) {
String s1 = cin.next();
String s2 = cin.next();
char[] c1 = s1.toCharArray();
char[] c2 = s2.toCharArray();
// System.out.println(s1+" "+s2);
build(c1, c2, 0, 0, c1.length);
System.out.println("");
}
}
private static void build(char[] c1, char[] c2, int i, int j, int n) {
if (n <= 0)
return;
char pre = c1[i];
int p = 0;
while (p < n && !(pre == c2[j + p])) {// find p inList's Pos
p++;
}
build(c1, c2, i + 1, j, p); // left tree build
build(c1, c2, i + 1 + p, j + p + 1, n - 1 - p); // right tree build
System.out.print(pre+" ");
}
}