UVA 10098 Generating Fast, Sorted Permutation

// 给你字符串 按字典序输出所有排列
// 要是每个字母都不同 可以直接dfs ^_^
// 用前面说的生成排列算法 也可以直接 stl next_permutation

#include <iostream> #include <string> #include<sstream> #include <cmath> #include <map> #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; char s[100]; int n; void change(int l,int r) { while(l<r) { swap(s[l],s[r]); l++; r--; } } bool permutation() { int i=n-1; while(i>0&&s[i-1]>=s[i]) i--; if(!i) return false; int k=i,j=n-1; for(;j>i;j--) if(s[j]>s[i-1]){ k=j; break; } swap(s[i-1],s[k]); change(i,n-1); return true; } int main() { int T; scanf("%d",&T); while(T--) { scanf("%s",s); n=strlen(s); sort(s,s+n); do { printf("%s ",s); }while(permutation()); printf(" "); } return 0; }
原文地址:https://www.cnblogs.com/372465774y/p/3603130.html