import java.util.Map;
import java.util.TreeMap;

public class LZWEncode {

    private String input;
    private Map codetable;
    private StringBuffer result;

    public LZWEncode(String input) {
        result = new StringBuffer();
        codetable = new TreeMap();
        this.input = input;
        initCodetable();
        encode();
        System.out.println(result.toString());
    }

    private void initCodetable() {
        for (int i = 0; i < 256; i++) {
            codetable.put(Character.toString((char) i),
                    new Integer(codetable.size()));
        }
    }

    private void encode() {
        String w = "";
        for (int i = 0; i < input.length(); i++) {
            char x = input.charAt(i);
            String wx = w + x;
            if (codetable.containsKey(wx)) {
                w = wx;
            } else {
                codetable.put(wx, new Integer(codetable.size()));
                result.append(codetable.get(w));
                w = Character.toString(x);
            }
        }
        result.append(codetable.get(w));
    }

    public static void main(String[] args) {
        new LZWEncode("abrakadabra");
    }
}