所有的排列,但是要不重复
#include<stdio.h> #include<iostream> #include<sstream> #include<queue> #include<map> #include<memory.h> #include <math.h> #include<time.h> #include <stdlib.h> #include <algorithm> using namespace std; #define N 12 int vis[N]; char a[N]; char b[N]; int length; int n; int final = 0; void dfs(int cur) { if(cur == length) { for(int i = 0; i < cur; i++) cout << b[i]; cout << endl; return; } for(int i = 0; i < length; i++) { if(!i || a[i] != a[i - 1]) { int c1 = 0, c2 = 0; for(int j = 0; j < cur; j++) if(b[j] == a[i]) c1++; for(int j = 0; j < length; j++) if(a[j] == a[i]) c2++; if(c1 < c2) { b[cur] = a[i]; dfs(cur + 1); } } } } void sort() { for(int i = 0; i < length; i++) { for(int j = 1; j < length - i; j++) { if(a[j] < a[j - 1]) { char cc = a[j]; a[j] = a[j - 1]; a[j - 1] = cc; } } } } int main(const int argc, char** argv) { freopen("d:\\1.txt", "r", stdin); cin >> n; while (n--) { memset(a, 0, sizeof(a)); scanf("%s", a); length = strlen(a); sort(); dfs(0); //if(n != 0) cout << endl; } return 0; }
#include<stdio.h> #include<iostream> #include<sstream> #include<queue> #include<map> #include<memory.h> #include <math.h> #include<time.h> #include <stdlib.h> #include <algorithm> using namespace std; #define N 12 int vis[N]; char a[N]; char b[N]; int length; int n; int final = 0; void dfs(int cur) { if(cur == length) { for(int i = 0; i < cur; i++) cout << b[i]; cout << endl; return; } for(int i = 0; i < length; i++) { if(!i || a[i] != a[i - 1]) { int c1 = 0, c2 = 0; for(int j = 0; j < cur; j++) if(b[j] == a[i]) c1++; for(int j = 0; j < length; j++) if(a[j] == a[i]) c2++; if(c1 < c2) { b[cur] = a[i]; dfs(cur + 1); } } } } void sort() { for(int i = 0; i < length; i++) { for(int j = 1; j < length - i; j++) { if(a[j] < a[j - 1]) { char cc = a[j]; a[j] = a[j - 1]; a[j - 1] = cc; } } } } int main(const int argc, char** argv) { freopen("d:\\1.txt", "r", stdin); cin >> n; while (n--) { memset(a, 0, sizeof(a)); scanf("%s", a); length = strlen(a); sort(); do { cout << a << endl; } while (next_permutation(a, a + length)); cout << endl; } return 0; }
想看库函数源码了。。。。。