#include<cstdio> #include<cstring> #include<algorithm> using namespace std; char s[60]; int l; typedef long long int64; int get(){ int i=l-1; while(i>0&&s[i-1]>=s[i]) i--; if(!i) return 0; int mp=i; for(int j=i+1;j<l;j++){ if(s[j]<=s[i-1]) continue; if(s[j]<=s[mp]) mp=j; } swap(s[mp],s[i-1]); sort(s+i,s+l); return 1; } int main(){ int casen; scanf("%d",&casen); while(casen--){ scanf("%s",&s); l=strlen(s); sort(s,s+l); printf("%s\n",s); while(get()) printf("%s\n",s); printf("\n"); } return 0; }
原文:http://www.cnblogs.com/dominating/p/5294125.html