#include<iostream>
using namespace std;
//算法一
/*
长度为n的字符串是长度为n-1的字符串 依次取(用循环实现) 长度为n-1的字符串的最后一个字符 后面的字符
依次递归 找出包含某个字串的所有组合
*/
//查找 长度为end+1的字符串的最后一个字符 在 原始串中的位置
int IndexOfString(char str[],int length,char temp[],int end){
for(int i=end;i<length;i++){
if(str[i]==temp[end])
return i;
}
return 0;//抛出异常 似乎更合理
}
void ZuHe(char str[],int length,char temp[],int size){
if(size>=length)return;
for(int j=0;j<=size;j++)
cout<<temp[j];
cout<<endl;
int n=0;
n=IndexOfString(str,length,temp,size)+1;
++size;
for(int i=n;i<length;i++){
temp[size]=str[i];
ZuHe(str,length,temp,size);
}
}
void ZuHe(char str[],int length,char * temp){
for(int i=0;i<length;i++){
temp[0]=str[i];
ZuHe(str,length,temp,0);
}
}
void main(){
char str[]="abce";
int length=4;
char *temp=new char[length+1];
temp[length]='\0';
ZuHe(str,length,temp);
}
字符串的所有组合
最新推荐文章于 2020-10-07 14:25:15 发布