Normand Briere
2019-09-02 21ac57b36a9e3b909853c7d64ac29b7ad72490a3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
 
import java.util.Hashtable;
 
public class LZW
{
 
    int count;
    
    LZW()
    {
        inputDict = new Hashtable();
        outputDict = new Hashtable();
 
        w = new StringBuffer("");
        v = new StringBuffer("");
        
        count = 0;
    }
 
    /**/
    public int Encode(char k) // throws DoNotSend
    {
        int temp;
        String s = "" + w + k;
        if (inputDict.containsKey(s))
        {
            w = w.append(k);
//            throw new DoNotSend();
            return -1;
            /*
            Integer grrr = (Integer) inputDict.put(s,s);
            inputDict.put(grrr,s);
            return grrr.intValue();
             */
        } else
        {
            Integer val = new Integer(count);
            inputDict.put(s, val);
            outputDict.put(val, s);
            w = new StringBuffer("" + k);
            return count++;
            /*
            Integer K = (Integer)inputDict.get("" + w);
            temp = K.intValue();
            return temp;
             */
        }
    }
 
    public String Decode(int k)
    {
        Integer K = new Integer(k);
        return (String)outputDict.get(K);
 
        /*
        String entry;
 
        if (v.length() == 0)
        {
            v = new StringBuffer((String)outputDict.get(new Integer(k)));
            return (v.toString());
        } else
        {
            entry = new String((String)outputDict.get(K));
 
            if (entry.length() == 0)
            {          // remove this conditional to cause LZW bug
                entry = new String(v.toString() + v.charAt(0));
            }
 
            outputDict.put(K, "" + v + entry.charAt(0));
 
            v = new StringBuffer(entry);
            return entry;
        }
         */
    }
    
    Hashtable inputDict;
    Hashtable outputDict;
    StringBuffer w;
    StringBuffer v;
}
 
class DoNotSend extends Exception
{
}