/* 2chエンコーダ完動ソース (3/3) */
unsigned char buffer[BLOCK_SIZE];
long list[BLOCK_SIZE], head[ALPHA_SIZE];

int main(int argc, char *argv[])
{
 int i, count;
 unsigned a;
 long p;

 for (i = 0; i < ALPHA_SIZE; i++)
  mtf_array[i] = i;
 while ((count = fread(buffer, 1, BLOCK_SIZE, stdin)) > 0) {
  mtf_write(buffer[0]);
  buffer[count] = buffer[0];
  for (i = 0; i < ALPHA_SIZE; i++)
   head[i] = -1;
  for (i = count - 1; i >= 0; i--) {
   a = buffer[i];
   list[i] = head[a];
   head[a] = i;
  }
  for (i = 0; i < ALPHA_SIZE; i++) {
   for (p = head[i]; p >= 0; p = list[p])
    mtf_write(buffer[p + 1]);
  }
 }
 flush_bits();
 return 0;
}