import choco.Problem;
import choco.Constraint;
import choco.Solver;
import choco.Solution;
import choco.ContradictionException;
import choco.integer.IntVar;
import choco.search.AssignVar;

import java.io.*;
import java.lang.*;
import java.util.*;

public class RBuild {	
   

    public static void main(String[] args) throws Exception,ContradictionException,FileNotFoundException,IOException {
	Dict d = new Dict();
	Tree t1 = new Tree(args[0],d);
	Tree t2 = new Tree(args[1],d);
	Problem pb = new Problem();
	int m = Math.max(t1.getMaxDDate(),t2.getMaxDDate());
	UMatrix um = new UMatrix(m,pb,d,args[2].equals("best"));	    

	t1.getRanks(um);
	t1.breakUp(um);

	
	t2.getRanks(um);
	t2.breakUp(um);	

	Solver sol = pb.getSolver();

	sol.setVarSelector(new VarOrder(um.getDecisionVars()));

	if (args[2].equals("best")) {
	    pb.minimize(um.getSum(),true);
	    //System.out.println(um);
	    Tree t3 = um.toTree();
	    System.out.println(t3);
	}
	else if (args[2].equals("all")){
	    if (pb.solve().booleanValue()){
		//System.out.println(um);
		Tree t3 = um.toTree();
		System.out.println(t3);
		while (pb.nextSolution().booleanValue()) {
		    //System.out.println(um);
		    t3 = um.toTree();
		    System.out.println(t3);
		}
	    }
	}
	else if (args[2].equals("first")){
	    //pb.propagate(); //System.out.println(um);
	    pb.solve();
	    //System.out.println(um);
	    Tree t3 = um.toTree();
	    System.out.println(t3);
	}
    }
}
	    
