1. 程式人生 > >運用遞迴求字串最大重複數

運用遞迴求字串最大重複數

void MaxLengthCore(char *cur,char *pre,int *max,int cursum)
{
if(*cur == '\0')
return ;
if(pre !=NULL && *cur == *pre)
{
++cursum;
if(cursum > *max)
*max = cursum;
pre = cur;
MaxLengthCore(++cur,pre,max,cursum);
}
else
{
cursum = 1;
pre = cur;
MaxLengthCore(++cur,pre,max,cursum);
}
}

int MaxLength(char
*a,int n) { assert(a!=NULL && n>0); int *max = (int*)malloc(sizeof(int)); *max = 1; int cursum = 1; char *pre = NULL; MaxLengthCore(a,pre,max,cursum); return *max; }