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("]\n");
} 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);
free(code);
return 0;
}

  上面算法不是错的,根据题意和不同的思考角度可以编写不同的mtf算法,按字典序排序后的mtf算法(只统计出现的字符):

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

  rle算法(了解该算法:https://en.wikipedia.org/wiki/Run-length_encoding):

const char*digits = "0123456789";

void comp_bit(char *rle, int *i, int num)
{
if (num > 9)
comp_bit(rle, i, num / 10);
rle[(*i)++] = digits[num%10];
} void rle_encode(const char *s, char *rle)
{
unsigned i = 0, j = 1;
rle[i++] = *s++;
while (*s)
{
if (rle[i - 1] != *s)
{
comp_bit(rle, &i, j);
rle[i++] = *s, j = 1;
}
else j++;
s++;
}
comp_bit(rle, &i, j);
}

  

  

Move-to-front(MTF) and Run-lenght encoding(RLE) algorithms的更多相关文章

  1. poj-1782 Run Length Encoding

    http://poj.org/problem?id=1782 Run Length Encoding Time Limit: 1000MS   Memory Limit: 30000K Total S ...

  2. Gym 100342F Move to Front (树状数组动态维护和查询)

    用树状数组动态和查询修改排名. 树状数组可以很方便地查询前缀和,那么可以利用这一特点,记录一个点在树状数组里最后一次出现的位置, 查询出这个位置,就可以知道这个点的排名了.更改这个点的排名的时候只要把 ...

  3. SDUT 2352 Run Length Encoding

    点我看题目 题意 :将给定的字符串编码,编码的规则根据两条,1.如果字符串里有连续相等的字符,就变为两个字符,一个是这些连续相同的字符的个数,另一个是这个字符,但是如果数量超过了9个,那就输出9再输出 ...

  4. UVW源码漫谈(番外篇)—— Emitter

    这两天天气凉了,苏州这边连续好几天都是淅淅沥沥的下着小雨,今天天气还稍微好点.前两天早上起来突然就感冒了,当天就用了一卷纸,好在年轻扛得住,第二天就跟没事人似的.在这里提醒大家一下,天气凉了,睡凉席的 ...

  5. hive_学习_01_hive环境搭建(单机)

    一.前言 本文承接上一篇:hbase_学习_01_HBase环境搭建(单机),主要是搭建 hive 的单机环境 二.环境准备 1.说明 hive 的下载来源有: 官方版本:http://archive ...

  6. UVA 1351 十三 String Compression

    String Compression Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit ...

  7. COCO 数据集的使用

    Windows 10 编译 Pycocotools 踩坑记 COCO数据库简介 微软发布的COCO数据库, 除了图片以外还提供物体检测, 分割(segmentation)和对图像的语义文本描述信息. ...

  8. 北大poj- 1009

    Edge Detection Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22835   Accepted: 5398 D ...

  9. 自制 COCO api 直接读取类 COCO 的标注数据的压缩文件

    第6章 COCO API 的使用 COCO 数据库是由微软发布的一个大型图像数据集,该数据集专为对象检测.分割.人体关键点检测.语义分割和字幕生成而设计.如果你要了解 COCO 数据库的一些细节,你可 ...

随机推荐

  1. Android 开发OOM解决方案

    OOM(Out Of Memory)在加载图片过多或者过大的情况下会发生OOM,可以查看APP最高可用内存: int maxMemory = (int) (Runtim.getRuntime().ma ...

  2. 关于spring boot集成MQTT

    安装 说到mqtt,首先肯定要安装了,安装什么的地址:http://activemq.apache.org/ap...我本地是Windows的环境,所以装的是Windows版本,这里是第一个注意的地方 ...

  3. [转] UML中的六大关系

    UML中的六大关系 转自:https://www.cnblogs.com/hoojo/p/uml_design.html 在UML类图中,常见的有以下几种关系: 泛化(Generalization), ...

  4. Git 添加远程github仓库的时候提示错误:fatal: remote origin already exists.

    1.先删除远程 Git 仓库 $ git remote rm origin 2.再添加远程 Git 仓库 $ git remote add origin git@github.com:wsydxian ...

  5. BeautifulSoup的基本使用

    一.将一段文档传入BeautifulSoup的构造方法,得到一个文档的对象: from bs4 import BeautifulSoup Soup = BeautifulSoup(html_doc) ...

  6. css实现移动端滚动条隐藏仍然可以滚动内容

    .g-panel { height: calc(100% - 112px); overflow: auto; &::-webkit-scrollbar { display: none; // ...

  7. AcWing 896. 最长上升子序列 II

    #include<iostream> #include<algorithm> #include<vector> using namespace std; int m ...

  8. 阿里配置docker镜像专属地址

    阿里配置docker镜像专属地址 待办 https://www.jianshu.com/p/6b416dff0691

  9. 前端分页神器,jquery grid的使用(前后端联调),让分页变得更简单。

    jquery grid 是一款非常好用的前端分页插件,下面来讲讲怎么使用. 首先需要引入jquery grid 的CSS和JS (我们使用的是bootstrap的样式) 下面我们通过一个例子来讲解,需 ...

  10. codeforces 597div2 F. Daniel and Spring Cleaning(数位dp+二维容斥)

    题目链接:https://codeforces.com/contest/1245/problem/F 题意:给定一个区间(L,R),a.b两个数都是属于区间内的数,求满足 a + b = a ^ b ...