Share 443. string compression c++,

buuchuong114

New member
#Nén, #C ++, #data nén, #AlGorithM, #Programming ## 443. Nén chuỗi trong C ++

Nén chuỗi là một kỹ thuật để giảm số lượng byte cần thiết để lưu trữ một chuỗi.Điều này có thể hữu ích để tiết kiệm không gian trên đĩa hoặc trong bộ nhớ hoặc truyền dữ liệu qua mạng.Có một số thuật toán nén chuỗi khác nhau, mỗi thuật toán có ưu điểm và nhược điểm riêng.

Trong bài viết này, chúng tôi sẽ thảo luận về thuật toán 443 để nén chuỗi trong C ++.Thuật toán này là một thuật toán đơn giản nhưng hiệu quả có thể đạt được tỷ lệ nén tốt trên nhiều chuỗi đầu vào khác nhau.

### Thuật toán 443

Thuật toán 443 hoạt động bằng cách lặp lại chuỗi đầu vào, tìm kiếm các chuỗi con lặp đi lặp lại.Khi tìm thấy một chuỗi con lặp đi lặp lại, nó được thay thế bằng một mã hóa duy nhất đại diện cho độ dài của chuỗi con.Ví dụ: nếu chuỗi đầu vào chứa "ABC", nó sẽ được thay thế bằng mã hóa "3".

Thuật toán 443 tiếp tục quét chuỗi đầu vào cho đến khi không thể tìm thấy chất nền lặp lại nhiều hơn.Chuỗi kết quả sau đó là biểu diễn nén của chuỗi gốc.

### Ví dụ

Hãy xem xét một ví dụ về thuật toán 443 đang hoạt động.Xem xét chuỗi đầu vào sau:

`` `
ABCABCABC
`` `

Thuật toán 443 trước tiên sẽ quét chuỗi cho các chuỗi con lặp lại.Nó sẽ tìm thấy "ABC" được lặp đi lặp lại ba lần, vì vậy nó sẽ thay thế ba lần xuất hiện này bằng mã hóa "3".Chuỗi nén kết quả sẽ là:

`` `
333
`` `

### Thực hiện

Thuật toán 443 có thể được thực hiện trong C ++ bằng các bước sau:

1. Tạo một bảng tra cứu ánh xạ chiều dài chuỗi con lên các mã hóa.
2. Khởi tạo bảng tra cứu với các mã hóa sau:
* 1: "A"
* 2: "AB"
* 3: "ABC"
3. Quét chuỗi đầu vào cho các chuỗi con lặp lại.
4. Khi tìm thấy một chuỗi con lặp đi lặp lại, hãy thay thế nó bằng mã hóa tương ứng từ bảng tra cứu.
5. Chuỗi kết quả là biểu diễn nén của chuỗi gốc.

Mã sau đây cho thấy việc thực hiện hoàn chỉnh thuật toán 443 trong C ++:

`` `C ++
#include <Istream>
#include <String>

sử dụng không gian tên STD;

// Một bảng tra cứu ánh xạ chiều dài chuỗi con lên các mã hóa.
const char* lookup_table [] = {
"Một",
"AB",
"ABC"
};

// nén chuỗi đã cho bằng thuật toán 443.
Chuỗi nén (chuỗi str) {
// Khởi tạo chuỗi nén.
Chuỗi nén_str = "";

// Quét chuỗi đầu vào cho các chuỗi con lặp lại.
for (int i = 0; i <str.length (); i ++) {
// Kiểm tra xem ký tự hiện tại là khởi đầu của một chuỗi con lặp đi lặp lại.
if (i + 1 <str.length () && str == str [i + 1]) {
// Tìm độ dài của chuỗi con lặp lại.
int len = 2;
while (i + len <str.length () && str == str [i + len]) {
Len ++;
}

// Thêm mã hóa cho chuỗi con lặp lại vào chuỗi nén.
nén_str += Lookup_table [len];

// Bỏ qua chuỗi con lặp đi lặp lại.
i += len - 1;
} khác {
// Thêm ký tự hiện tại vào chuỗi nén.
nén_str += str ;
}
}

// Trả về chuỗi nén.
trả về nén_str;
}

int main () {
// Nhận chuỗi đầu vào từ người dùng.
chuỗi str;
cin >> str;

// Nén chuỗi bằng thuật toán 443.
Chuỗi nén_str = nén (str);

// In
=======================================
#String compression, #C++, #data compression, #AlGorithM, #Programming ## 443. String Compression in C++

String compression is a technique for reducing the number of bytes required to store a string. This can be useful for saving space on disk or in memory, or for transmitting data over a network. There are a number of different string compression algorithms, each with its own advantages and disadvantages.

In this article, we will discuss the 443 algorithm for string compression in C++. This algorithm is a simple but effective algorithm that can achieve good compression ratios on a wide variety of input strings.

### The 443 Algorithm

The 443 algorithm works by iteratively scanning the input string, looking for repeated substrings. When a repeated substring is found, it is replaced with a single codeword that represents the length of the substring. For example, if the input string contains the substring "abc", it would be replaced with the codeword "3".

The 443 algorithm continues to scan the input string until no more repeated substrings can be found. The resulting string is then the compressed representation of the original string.

### Example

Let's look at an example of the 443 algorithm in action. Consider the following input string:

```
abcabcabc
```

The 443 algorithm would first scan the string for repeated substrings. It would find the substring "abc" repeated three times, so it would replace these three occurrences with the codeword "3". The resulting compressed string would be:

```
333
```

### Implementation

The 443 algorithm can be implemented in C++ using the following steps:

1. Create a lookup table that maps substring lengths to codewords.
2. Initialize the lookup table with the following codewords:
* 1: "a"
* 2: "ab"
* 3: "abc"
3. Scan the input string for repeated substrings.
4. When a repeated substring is found, replace it with the corresponding codeword from the lookup table.
5. The resulting string is the compressed representation of the original string.

The following code shows a complete implementation of the 443 algorithm in C++:

```c++
#include <iostream>
#include <string>

using namespace std;

// A lookup table that maps substring lengths to codewords.
const char* lookup_table[] = {
"a",
"ab",
"abc"
};

// Compresses the given string using the 443 algorithm.
string compress(string str) {
// Initialize the compressed string.
string compressed_str = "";

// Scan the input string for repeated substrings.
for (int i = 0; i < str.length(); i++) {
// Check if the current character is the start of a repeated substring.
if (i + 1 < str.length() && str == str[i + 1]) {
// Find the length of the repeated substring.
int len = 2;
while (i + len < str.length() && str == str[i + len]) {
len++;
}

// Add the codeword for the repeated substring to the compressed string.
compressed_str += lookup_table[len];

// Skip over the repeated substring.
i += len - 1;
} else {
// Add the current character to the compressed string.
compressed_str += str;
}
}

// Return the compressed string.
return compressed_str;
}

int main() {
// Get the input string from the user.
string str;
cin >> str;

// Compress the string using the 443 algorithm.
string compressed_str = compress(str);

// Print
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top