Skip to content

Instantly share code, notes, and snippets.

@KT-Yeh
Created December 30, 2013 13:34
Show Gist options
  • Save KT-Yeh/8182153 to your computer and use it in GitHub Desktop.
Save KT-Yeh/8182153 to your computer and use it in GitHub Desktop.
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <vector>
using namespace std;
void permu(int all,int left,char str[],bool choosed[],vector<char> &temp);
bool cmp(char a,char b){
return a<b;
}
/*
temp[]來存已選擇的字元
choosed[]來表示哪些字元已被選擇
all表示總共幾個字元
left表示剩下幾個字元還沒選到
*/
int main()
{
int n;
char str[15];
scanf("%d",&n);
while(n--){
scanf("%s",str);
sort(str,str+strlen(str),cmp);
bool choosed[15]={0};
vector<char> temp;
permu(strlen(str),strlen(str),str,choosed,temp);
printf("\n");
}
return 0;
}
void permu(int all,int left,char str[],bool choosed[],vector<char> &temp)
{
if(left==0){
for(int i=0;i<all;i++)
printf("%c",temp[i]);
printf("\n");
}
else{
for(int i=0;i<all;i++){
//避免aab會重複的問題
if(choosed[i]==0 && choosed[i+1]==1 && str[i]==str[i+1]) continue;
if(choosed[i]==0){
choosed[i]=1;
temp.push_back(str[i]);
permu(all,left-1,str,choosed,temp);
temp.pop_back();
choosed[i]=0;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment