Trang

Chủ Nhật, 2 tháng 1, 2011

1144. Tree Recovery

import java.io.*;

public class Main {

    /**
     * http://acm.tju.edu.cn/toj/showp1144.html
     * @author hunglee
     */
    public static void main(String[] args) throws IOException {

        StreamTokenizer in = new StreamTokenizer(new BufferedReader(
                new InputStreamReader(System.in)));

        String preord, inord;
        while (in.nextToken() != StreamTokenizer.TT_EOF) {
            preord = in.sval;
            in.nextToken();
            inord = in.sval;
            process(preord, inord);
            System.out.println();
        }
    }

    private static void process(String preord, String inord) {

        if (preord.length() <= 1) {
            System.out.print(preord);
            return;
        }
        int i = inord.indexOf(preord.charAt(0));
        process(preord.substring(1, i + 1), inord.substring(0, i));
        process(preord.substring(i + 1), inord.substring(i + 1));
        System.out.print(preord.charAt(0));
    }

}

Không có nhận xét nào:

Đăng nhận xét