A common problem with moving-to-front encoding: putting an element at the beginning

Source: Internet
Author: User

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 ();} 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.