Skip to main content

A Simple String Compression Algorithm

Thuật toán đơn giản dùng để giảm dung lượng bộ nhớ để lưu string
Giả sử ta cần lưu string như thế này: string1 =  "ABCDAAB"
Dễ thấy, với một ký tự trong string ta cần 1 byte hay 8 bít để biểu diễn
Vậy string1 sẽ cần 7 byte (7*8 = 56 bít) trong bộ nhớ.
Vậy làm thế nào để lưu string này với ít hơn 7 byte?
Cách 1:
1. Dễ thấy string 1 được biểu diễn bới 4 ký tự: 'A', 'B', 'C', 'D'
Bước 1: Lập 1 bảng hằng số để định nghĩa lại các ký tự này
A là số 1 => A = 00000001
B là số 2 => B = 00000010
C là số 3 => C = 00000011
D là số 4 => D = 00000100
Bước 2: Nhìn bảng, ta thấy mỗi ký tự chỉ cần 3 bít để biểu diễn
A là số 1 => A = 001
B là số 2 => B = 010
C là số 3 => C = 011
D là số 4 => D = 100
Bước 3: Biểu diễn string1 theo bảng hằng số vừa được định nghĩa
before: string1 = A     B   C     D   A    A    B
after  :  string1 = 001 010 011 100 001 001 010
Vậy rõ ràng, sau khi biến đổi, ta chỉ cần 3*7 = 21 bít (<56 bít) để biểu diễn string1
Cách 2:
For example, consider a screen containing plain black text on a solid white background. There will be many long runs of white pixels in the blank space, and many short runs of black pixels within the text. Let us take a hypothetical single scan line, with B representing a black pixel and W representing white:
WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW
If we apply the run-length encoding (RLE) data compression algorithm to the above hypothetical scan line, we get the following:
12W1B12W3B24W1B14W
Cách này cũng có ưu điểm và nhược điểm nhất định, tùy từng trường hợp mà áp dụng cho phù hợp

Reference:



Comments

Popular posts from this blog

[Git] Handle trailing space when patching with git

After complete coding one module in development branch. Next phase is merging. Obviously, you can merge source code automatically without no errors happen. However, life is not dream. Create a patch git diff HEAD > newTariff.patch Apply patch git apply newTariff.patch then problem happens Problem git apply newTariff.patch:106: trailing whitespace. patch does not apply Route cause: Nguyên nhân: Do trong source code có những dấu space thừa (ô vuông màu đỏ hình dưới) Some whitespaces is existed in your patch. (red area in below pictures) Fix Clean white space and patch again. Cách khắc phục Xóa những dấu space này đi và thực hiện patch lại Make up after complete coding. Find and clean whitespace before create patch file. Sửa sau khi code xong Kiểm tra sau khi code xong có lỗi này không git diff HEAD --check Prevent whitespace by manually when typing source code Sửa ngay khi đang code =>Bật chức năng hiển thị các dấu whitespace, hoặc remove trailing ...

How to test frame buffer in linux

The intended goal of this article is to provide the user with enough information so they can display an image onto a screen of their choosing. How to re-build frame buffer testing application. Base knowledge http://trac.gateworks.com/wiki/OpenEmbedded/Video_Out Frame buffer test application - rebuild fb-test-app step 1: Download soure code ( git installed on your machine and account in git-hub is required) https://github.com/prpplague/fb-test-app Command in git: git init git clone https://github.com/prpplague/fb-test-app.git step 2: Change makefile vi Makefile //open Makefile by vim editor :set nu //show line number replace from line 6 to line 12 by folow code: CC=arm-linux-gnueabi-gcc CFLAGS=-02 -Wall Save change command :wq step 3: Rebuid Type “make” command step 4: Copy execute file to target system by filezilla or something like that step 5: Remote target by root account, change mode of execute file (what copied from host) ...

[GTK] Example of using GtkBox with label and button

[1]Source code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 #include <gtk/gtk.h> void on_window_closed (GtkWidget * window, gpointer data) { gtk_main_quit(); } static void destroy (GtkWidget * widget, gpointer data) { gtk_main_quit(); } static void button_clicked (GtkWidget * button, gpointer data) { g_print( "Button clicked \n " ); } int main ( int argc, char * argv[]) { GtkWidget * window, * label, * box, * button ; gtk_init( & argc, & argv); window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_title(GTK_WINDOW (window), "Test GTK" ); gtk_window_set_default_size (GTK_WINDOW (window), 500 , 200 ); #if 1 //Use gtk3: change 0->1, Use gtk2: keep it is 0 //Using gtk3 box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5 ); ...