首页 > 其他 > 详细

UVA 10098 Generating Fast, Sorted Permutation

时间:2014-03-17 07:39:52      阅读:387      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
// 给你字符串 按字典序输出所有排列
// 要是每个字母都不同 可以直接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; }
bubuko.com,布布扣

UVA 10098 Generating Fast, Sorted Permutation,布布扣,bubuko.com

UVA 10098 Generating Fast, Sorted Permutation

原文:http://www.cnblogs.com/372465774y/p/3603130.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!