In the encoding of MTF, it is mainly to compare the string to encode with the symbol table, save the corresponding index, and put the same value at the beginning of the symbol table.
Eg: String = caaabaa...
Symbol table = 0123abcd...
To get: c0123abd...
Therefore, the main problem is to switch the middle section to the C segment.
Method 1: two cycles. For the first time, compare one by one in the symbol table and find the IND of the string element in the St. 2nd times, the same elements found in St are exchanged one by one with the previous one, knowing that it is switched to the beginning.
Method 2: MSI: only one cycle is used, and you can draw a step by step. Great use of for loop writing and temp var. Tmpout is witty.
Public Static Void Encode () Throws Ioexception { Char [] Chars = Radixlist (); Char Count, CH, tmpin, tmpout; While (! Binarystdin. isempty () {ch = Binarystdin. readchar (); For (COUNT = 0, tmpout = chars [0]; ch! = Chars [count]; count ++ ) {Tmpin = Chars [count]; chars [count] = Tmpout; tmpout =Tmpin;} chars [count] = Tmpout; binarystdout. Write (count); chars [ 0] = Ch;} binarystdout. Close ();}