Run-length encoding (compression) · ترميز الطول المتتابع (الضغط)
Squeezing repeated data
- Compression makes data smaller. Run-length encoding (RLE) is one simple way.
- It works well when the same value repeats many times in a row.
- It is lossless: from the squeezed form you can rebuild the exact original.
ضغط البيانات المكررة
- الضغط يجعل البيانات أصغر. ترميز طول التسلسل (RLE) هو طريقة بسيطة واحدة.
- يعمل بشكل جيد عندما تتكرر نفس القيمة مرات عديدة متتالية.
- هو بدون فقدان: من الشكل المضغوط يمكنك إعادة بناء الأصل الدقيق.
What a "run" is
- A run is a stretch of the same character repeated: in
"aaabbc","aaa"is a run of 3. - RLE replaces each run with the character followed by how many times it repeats.
- So
"aaabbc"becomes"a3b2c1"— much shorter when runs are long.
ما هو "التسلسل"
- التسلسل هو سلسلة من نفس الحرف مكرر: في
"aaabbc"،"aaa"هو تسلسل بطول 3. - يقوم RLE باستبدال كل تسلسل بالحرف متبوعًا بـ كم مرة يتكرر.
- لذا يصبح
"aaabbc""a3b2c1"— أقصر بكثير عندما تكون التسلسلات طويلة.
Counting a run
- To measure a run, look at a character, then count how many of the same character follow it.
- Stop when the next character is different, or you reach the end (
'\0'). - That count is the run's length.
عدّ التسلسل
- لقياس تسلسل، انظر إلى حرف، ثم عد كم عدد الأحرف نفسه التي تليه.
- توقف عندما يكون الحرف التالي مختلفًا، أو تصل إلى النهاية (
'\0'). - هذا العداد هو طول التسلسل.
Building the encoded string
- Walk the input. For each run, write the character, then its count, into the output.
- Move your input position past the whole run before starting the next one.
- End the output string with
'\0'so it is a proper C string.
بناء السلسلة المشفرة
- مرر على المدخلات. لكل تسلسل، اكتب الحرف، ثم عدده، في المخرجات.
- حرك موضع الإدخال الخاص بك بعد كامل التسلسل قبل البدء في التالي.
- أنهِ سلسلة المخرجات بـ
'\0'لتكون سلسلة C صحيحة.
#include <stdio.h>
int main(void) {
const char *s = "aaab";
int i = 0;
char c = s[i];
int count = 0;
while (s[i] == c) { // count the first run
count++;
i++;
}
printf("%c%d\n", c, count); // a3
return 0;
}
Common mistakes
- Run-length encoding stores a value then its count; it only helps when there are long runs.
- It is lossless — the original is restored exactly.
أخطاء شائعة
- يخزن ترميز طول التسلسل قيمة ثم عددها؛ وهو مفيد فقط عندما تكون هناك تسلسلات طويلة.
- هو بدون فقدان — يتم استعادة الأصل بدقة.
Now you try
- Find each run, then write the character and its count to the output.
- The caller gives you an output buffer big enough to hold the result. Do not write a
main.
الآن جرب بنفسك
- ابحث عن كل تسلسل، ثم اكتب الحرف وعدده في المخرجات.
- يُعطيك المُدْعي مصفوفة مخرجات كبيرة بما يكفي لاستيعاب النتيجة. لا تكتب
main.
Run-length encoding · ترميز المسارات
Replace a run of repeats with count + symbol — lossless. · استبدل سلسلة التكرارات بـ عدد + الرمز — بدون فقدان البيانات.
Complete int run_length_at(const char *s, int i) so it returns how many times the character s[i] repeats starting at index i. Do not write a main. · أكمل int run_length_at(const char *s, int i) لإرجاع عدد مرات تكرار الحرف s[i] بدءاً من الفهرس i. لا تكتب a main.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Complete void rle_encode(const char *in, char *out) so it writes the run-length encoding of in into out: each run becomes the character then its count. "aaabbc" becomes "a3b2c1". End out with '\0'. Do not write a main. · أكمل void rle_encode(const char *in, char *out) بحيث يكتب ترميز الطول المتتابع لـ in في out: كل سلسلة تصبح الحرف ثم عددها. "aaabbc" تصبح "a3b2c1". أنهِ out بـ '\0'. لا تكتب ⟩ main.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.