Created
September 30, 2014 14:43
-
-
Save 0V/efc245ec32f195c17279 to your computer and use it in GitHub Desktop.
再帰呼び出しの練習:RLE
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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