求助,有一道题我怎么也做不出来
输入一个长为2^k(k≤8)01串s,按照"ABC编码规则"进行编码,ABC编码规则是:{ A //若s串全是0
T(s)={ B //若s串全是1
{ CT(s1)(s2) //否则把s串分成两个等长的字串s1和s2
例如:
T(01001011)
=CT(0100)T(1011)
=CCT(01)T(00)CT(10)T(11)
=CCCT(0)T(1)ACCT(1)T(0)B
=CCCABACCBAB
#include <stdio.h> #include <stdlib.h> #include <math.h> void code_ABC(char *beg, char *end, char *res); int main() { int k, n; char *src, *res; printf("Enter k: "); scanf("%d", &k); n = (int)pow(2, k); if ((src=(char*)malloc(n)) == NULL) exit(1); if ((res=(char*)malloc(n+k)) == NULL) exit(1); printf("Enter the string [0|1]: "); scanf("%s", src); code_ABC(src, src+n, res); printf("The code result is: %s", res); return 0; } void code_ABC(char *beg, char *end, char *res) { static int i = 0; char *p = beg; while (p < end && *p == *beg) p++; if (p < end) { res[i] = 'C'; res[++i] = '\0'; code_ABC(beg, beg + (end-beg)/2, res); code_ABC(beg + (end-beg)/2, end, res); } else { res[i] = (*beg == '0' ? 'A' : 'B'); res[++i] = '\0'; } }