zoukankan      html  css  js  c++  java
  • [Jobdu] 题目1369:字符串的排列

    题目描述:

    输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串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

    再复习一下next_permutation,最后一组测试数据9位,用cout超时,关键时候还是得printf啊。

     1 #include <iostream>
     2 #include <string>
     3 #include <algorithm>
     4 #include <cstdio>
     5 using namespace std;
     6  
     7 bool next_permutation(string &s) {
     8     int n = s.length();
     9     bool res = false;
    10     if (n < 2) return res;
    11     int a, b;
    12     for (a = n - 2; a >= 0; --a) {
    13         if (s[a] < s[a + 1]) {
    14             res = true;
    15             break;
    16         }
    17     }
    18     for (b = n - 1; b > a; --b) {
    19         if (s[a] < s[b]) {
    20             break;
    21         }
    22     }
    23     char tmp = s[a];
    24     s[a] = s[b];
    25     s[b] = tmp;
    26     reverse(s.begin() + a + 1, s.end());
    27     return res;
    28 }
    29  
    30 int main() {
    31     string s;
    32     while (cin >> s) {
    33         sort(s.begin(), s.end());
    34         do {
    35             printf("%s
    ", s.c_str());
    36         } while (next_permutation(s));
    37     }
    38     return 0;
    39 }
    40 /**************************************************************
    41     Problem: 1369
    42     User: hupo250
    43     Language: C++
    44     Result: Accepted
    45     Time:150 ms
    46     Memory:1520 kb
    47 ****************************************************************/
  • 相关阅读:
    获得ip地理信息的几种方法
    设置 phoneGap/Cordova 3.4 应用程序启动动画闪屏 SplashScreen
    backbone实例01
    即使世界明天毁灭,我今天也要种下葡萄树
    真言真语
    美文美句
    25个实用的jQuery技巧和解决方案
    突袭HTML5之WebGL 3D概述
    敏捷开发之Scrum扫盲篇
    javascript中undefined和null的区别详解
  • 原文地址:https://www.cnblogs.com/easonliu/p/3645338.html
Copyright © 2011-2022 走看看