// 给你字符串 按字典序输出所有排列
// 要是每个字母都不同 可以直接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\n",s); }while(permutation()); printf("\n"); } return 0; }

 

相关文章:

  • 2021-11-14
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-11
  • 2021-06-09
  • 2022-03-03
猜你喜欢
  • 2021-07-01
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-16
相关资源
相似解决方案