首页 > 其他 > 详细

从Move-To-Front encoding的一个常用问题:把一个元素放到最前面

时间:2014-07-07 17:30:53      阅读:364      评论:0      收藏:0      [点我收藏+]

在MTF的encoding中,主要就是把要encode的string和symbol table比较,将对应的index保存并把相同的值放到symbol table的最前头。

eg: string = caaabaa...

       symbol table = 0123abcd...

则要得到:c0123abd...

所以主要问题就是把中间一段和c交换位置。

方法一:2次循环,第一次,在symbol table一个一个比较,找到string这个元素在ST中的ind。第2次,把ST中找到的相同元素一个一个的和前一个交换,知道它换到最前面。

方法二:msi的做法:只用一次循环,自己画了个step by step才想到。好厉害的for loop写法和temp var的运用。tmpout好机智。

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

 

从Move-To-Front encoding的一个常用问题:把一个元素放到最前面,布布扣,bubuko.com

从Move-To-Front encoding的一个常用问题:把一个元素放到最前面

原文:http://www.cnblogs.com/gpuasic/p/3813628.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!