码迷,mamicode.com
首页 > 其他好文 > 详细

Move-to-front(MTF) and Run-lenght encoding(RLE) algorithms

时间:2018-12-03 19:58:25      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:字符   pos   and   while   bsp   move   ring   length   abc   

  mtf算法(似乎有误,应该提前将不重复字符按字典序排序),关于该算法(https://www2.cs.duke.edu/csed/algoprobs/beta/bw1.html):

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void mtf_encode(const char *s, unsigned len, int *code)
{
    int pos = 0, num, trace = 0;
    unsigned i, j, k;
    int is_hav[26], stack[26];
	
    memset(is_hav, 0, 26);
	
    while (*s)
    {
        if (!is_hav[*s - ‘a‘])
        {
            stack[pos] = *s - ‘a‘ + 1;
            is_hav[*s - ‘a‘] = 1;
            ++pos;
            code[trace++] = pos;
        }
        else
        {
            j = 0;
            while (stack[j] != *s - ‘a‘ + 1) j++;
            code[trace++] = j + 1, num = stack[j];
            while (j > 0) stack[j] = stack[j - 1], j--;
            stack[j] = num;
        }
        s++;
    }
}

void print_mtf_encode(int*code, int len)
{
    unsigned i;
    printf("[");
    for (i = 0; i < len; i++)
    {
        printf("%d", code[i]);
        if (i != len - 1)
            printf(", ");
    }
    printf("]");
}

int main()
{
    char s[] = "abcabcaaaaaaaab";
    int len = strlen(s);
    int *code = (int *)malloc(sizeof(int) * len);
    mtf_encode(s, len, code);
    print_mtf_encode(code, len);
    return 0;
}

  rle算法,暂时没太明白。了解该算法:https://en.wikipedia.org/wiki/Run-length_encoding

 

Move-to-front(MTF) and Run-lenght encoding(RLE) algorithms

标签:字符   pos   and   while   bsp   move   ring   length   abc   

原文地址:https://www.cnblogs.com/darkchii/p/10060029.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!