一道题迷迷糊糊做了一天,等理清楚了再更新…
bool isHuiWen(char* s){
int length=strlen(s);
for(int i=0;i<length/2;i++)
if(s[i]!=s[length-1-i])
return false;
return true;
}
char* cut(char* s,int left,int right){
int length=0;
char* new=(char *)malloc(sizeof(char)*(right-left+2));
for(int i=left;i<=right;i++)
new[length++]=s[i];
new[length]='\0';
return new;
}
void dfs(char* s,int start,int len,char*** result,int* returnSize,int** returnColumnSizes,char** path,int pathSize){
if(start>=len){
result[*returnSize]=(char **)malloc(sizeof(char*)*pathSize);
for(int i=0;i<pathSize;i++)
result[*returnSize][i]=path[i];
(*returnColumnSizes)[(*returnSize)++]=pathSize;
return;
}
for(int i=start;i<len;i++){
char* temp=cut(s,start,i);
if(isHuiWen(temp)==true){
path[pathSize]=temp;
dfs(s,i+1,len,result,returnSize,returnColumnSizes,path,pathSize+1);
}
}
return;
}
char *** partition(char * s, int* returnSize, int** returnColumnSizes){
int len=strlen(s);
char*** result=(char***)malloc(sizeof(char**)*50000);
int* length=(int*)malloc(sizeof(int)*50000);
char** path=(char**)malloc(sizeof(char*)*len);
(*returnSize)=0;
(*returnColumnSizes)=(int*)malloc(sizeof(int)*50000);
dfs(s,0,len,result,returnSize,returnColumnSizes,path,0);
return result;
}