import java.io.*;
/**
   This class implements the decoding part of the 
   Ziv-Lempel text compression algorithm. Usage
   <code>java ZLdecoding source.txt dest.txt</code>
*/
public class ZLdecode{ 
    /**
       Initializes the dictionnary.
    */
    public static void init(String[] t){
	for(int i = 0; i<t.length; i++)
	    t[i] = new String();
    }
    /**
       Implements <code>ZLdecoding()</code>.
    */
    public static String ZLdecoding(String s){
	String res = new String();
	String[] table = new String[s.length()];
	init(table);
	int i =0; 
	int nTable = 1;
	while(i < s.length()-1){  //read (n,a)
	    int val = 0;
	    do{
		val = val * 10 + (s.charAt(i++)-'0');
	    }while(s.charAt(i) != ','); 
	    i++;  //read ','
	    char c = s.charAt(i++);  //read a
	    table[nTable++] = table[val] + c;
	    res = res + table[val] + c;
	}
	return res;
    }
    public static String fromFile(String name) throws IOException {
	FileReader fileIn = new FileReader(name);
	BufferedReader r = new BufferedReader(fileIn);
	String text = new String();
	String line;
	while ((line = r.readLine()) != null) {
	    text += line+"\n";}
	fileIn.close();
	System.out.println("input read");
	return text;
    }
    public static void toFile(String name, String s) throws IOException {
	FileWriter fileOut = new FileWriter(name);
	fileOut.write(s);
	//BufferedWriter w = new BufferedWriter(fileOut);
	// for( int i = 0; i < s.length(); i++)
	// 	    w.write(s.charAt(i));
	fileOut.close();

    }
    public static void main(String[] args) throws IOException {
	String s = ZLdecoding(fromFile(args[0]));
	System.out.println("decoding done");
	toFile(args[1], s);
    }
}






