字典比较,下一排列组合,上一排列组合

题目描述:

输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。

输入:

每个测试案例包括1行。

输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母。

输出:

对应每组数据,按字典序输出所有排列。

样例输入:
abc BCA 
样例输出:
abc acb bac bca cab cba ABC ACB BAC BCA CAB CBA

#include <iostream>
#include <string>
#include <stdio.h>
#include <algorithm>

using namespace std;

int main()
{
    string str;
    while(cin>>str)
    {
        sort(str.begin(),str.end());
        string tmp(str);
        do
        {
            //cout<<tmp<<endl;
            printf("%s\n",tmp.c_str());  //用printf()纯粹是为了提高打印速度.
            /*
            next_permutation(tmp.begin(),tmp.end());下一排列组合.
            prev_permutation(tmp.begin(),tmp.end());上一排列组合.

            */
            next_permutation(tmp.begin(),tmp.end());
        }while(lexicographical_compare(str.begin(),str.end(),tmp.begin(),tmp.end()));
        //字典比较.
    }
    return 0;
}

posted @ 2012-09-13 11:35  wmx3ng  阅读(262)  评论(0编辑  收藏  举报