public static void expand(){
  String[] st=new String[L];
  int i;
  for (i=0; i < R; i++)   st[i]="" + (char)i;
  st[i++]="";
  int codeword=BinaryStdIn.readInt(W);
  if (codeword == R)   return;
  String val=st[codeword];
  while (true) {
    BinaryStdOut.write(val);
    codeword=BinaryStdIn.readInt(W);
    if (codeword == R)     break;
    String s=st[codeword];
    if (i == codeword)     s=val + val.charAt(0);
    if (i < L)     st[i++]=val + s.charAt(0);
    val=s;
  }
  BinaryStdOut.close();
}

public static void expand ( ) { String [ ] st = new String [ L ] ; int i ; for ( i = 0 ; i < R ; i ++ ) st [ i ] = "" + ( char ) i ; st [ i ++ ] = "" ; int codeword = BinaryStdIn . readInt ( W ) ; if ( codeword == R ) return ; String val = st [ codeword ] ; while ( true ) { BinaryStdOut . write ( val ) ; codeword = BinaryStdIn . readInt ( W ) ; if ( codeword == R ) break ; String s = st [ codeword ] ; if ( i == codeword ) s = val + val . charAt ( 0 ) ; if ( i < L ) st [ i ++ ] = val + s . charAt ( 0 ) ; val = s ; } BinaryStdOut . close ( ) ; }
reads a sequence of bit encoded using lzw compression with 12 - bit codewords from standard input ; expands them ; and writes the results to standard output .
