Skip to content

Instantly share code, notes, and snippets.

@0V
Created September 30, 2014 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0V/efc245ec32f195c17279 to your computer and use it in GitHub Desktop.
Save 0V/efc245ec32f195c17279 to your computer and use it in GitHub Desktop.
再帰呼び出しの練習:RLE
#include<iostream>
using namespace std;
const int LENGTH = 200;
int char_count[LENGTH];
void set_str(char *dst, int n){
if (n == LENGTH){
return;
}
else if (char_count[n] > 0){
*dst = (char)n;
dst++;
*dst = (char)(char_count[n] + (int)'0');
dst++;
*dst = '\0';
n++;
set_str(dst, n);
}
else{
n++;
set_str(dst, n);
}
}
void encode_str(const char* str, char* dst){
if (*str == '\0'){
return;
}
else{
char_count[(int)*str]++;
++str;
encode_str(str, dst);
}
}
void run_length_encode(const char* str, char* dst){
encode_str(str, dst);
set_str(dst, 0);
}
int main(){
char str[] = "aakfushkfjhkjhkdsjhfa";
char dst[100];
run_length_encode(str, dst);
cout << dst << endl;
getchar();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment