1.字符串排列
void Permutation(string &str, int j, int n){
if(j==n){
cout<<str<<endl;
return;
}
for(int i=j;i<n;++i){
char temp=str[i];
str[i]=str[j];
str[j]=temp;
Permutation(str,j+1,n);
temp=str[i];
str[i]=str[j];
str[j]=temp;
}
}
void Permutation(string str){
int l=str.size();
if(l==0)
return;
Permutation(str,0,l);
}
2.字符串组合
void Combination(string str, int n, vector<char> &result,int j){
if(n==0){
for(vector<char>::iterator pos=result.begin();pos!=result.end();++pos)
cout<<*pos;
cout<<endl;
return;
}
if(j==str.size())
return;
result.push_back(str[j]);//将str[j]放入组合中,然后遍历下一个字符
Combination(str,n-1,result,j+1);
result.pop_back();
Combination(str,n,result,j+1);//将str[j]不放入组合中,然后遍历下一字符
}
void Combination(string str){
vector<char> result;
int l=str.size();
if(l==0)
return;
for(int i=1;i<=l;++i)//i个数的组合
Combination(str,i,result,0);
}