[c] 배열과 압축 알고리즘
이번 포스트에서는 배열과 압축 알고리즘에 대해 알아보겠습니다.
배열
배열은 동일한 데이터 타입을 지닌 요소들의 모음을 저장하는 자료구조입니다. 배열은 인덱스를 사용하여 각 요소에 접근할 수 있고, 요소의 추가, 제거, 수정 등의 작업이 가능합니다.
int numbers[5] = {1, 2, 3, 4, 5};
위 코드는 5개의 요소를 갖는 정수형 배열을 선언하고 초기화하는 예시입니다.
압축 알고리즘
압축 알고리즘은 데이터의 용량을 줄이는 알고리즘입니다. 주로 데이터를 더 작은 크기로 압축하여 저장하거나 전송할 때 사용됩니다. 그 중 대표적인 알고리즘으로는 LZ77, LZ78, LZW 등이 있습니다.
// 예시에서는 LZW 알고리즘을 사용하여 문자열을 압축하는 코드입니다.
#include <iostream>
#include <string>
#include <map>
std::string compress(const std::string& uncompressed) {
std::string compressed;
std::map<std::string, int> dictionary;
for (char c : uncompressed) {
std::string current = compressed + c;
if (dictionary.count(current)) {
compressed += c;
} else {
compressed += (char) (dictionary.size() + 256);
dictionary[current] = dictionary.size();
}
}
return compressed;
}
압축 알고리즘은 데이터 유실 없이 압축하고 복원할 수 있어야 합니다. 또한 오버헤드가 적고 신속한 처리가 가능해야 합니다.
이상으로 배열과 압축 알고리즘에 대한 간단한 소개였습니다. 감사합니다.
레퍼런스:
- https://en.wikipedia.org/wiki/Array_data_structure
- https://en.wikipedia.org/wiki/Data_compression_algorithm