美文网首页
poj1146 全排列(STL)

poj1146 全排列(STL)

作者: 暖昼氤氲 | 来源:发表于2019-11-05 16:44 被阅读0次
    /*
    Time:2019.11.5
    Author: Goven
    type:全排列 
    err:
    ref:
    知识点:1.原型:bool next_permutation(start_pos(迭代器), end_pos(迭代器)) 
              头文件:<algorithm>
              会改变传入参数 
            2.char a[80]; int len = strlen(a); //头文件 <cstring> 
    */
    #include<iostream>
    #include<algorithm> 
    #include<string>
    using namespace std;
    
    int main()
    {
        string str;
        while (cin >> str && str != "#") {
            int len = str.length();
            int cnt = len - 2;
            while (cnt >= 0 && str[cnt] >= str[cnt + 1]) {
                cnt--;
            }
            if (cnt < 0) cout << "No Successor" << endl;
            else {
                int i;
                for (i = len - 1; i >= 0 && str[i] <= str[cnt]; i--);
                char t = str[cnt];
                str[cnt] = str[i]; 
                str[i] = t;
                reverse(str.begin() + cnt + 1, str.end()); //一开始没有想到,直接翻转就可以,还在想着要重新排序,有点麻烦所以用char做了 
                cout << str << endl;
            }
        } 
        return 0;
    }
    
    //char
    #include<iostream>
    #include<algorithm>
    using namespace std;
    
    int main()
    {
        char a[55];
        while (cin >> a && a[0] != '#') {
            int len;
            for (len = 0; a[len] != '\0'; len++); 
            int cnt = len - 2;
            while (cnt >= 0 && a[cnt] >= a[cnt + 1]) {
                cnt--;
            }
            if (cnt < 0) cout << "No Successor" << endl;
            else {
                int i;
                for (i = len - 1; i >= 0 && a[i] <= a[cnt]; i--);
                char t = a[cnt];
                a[cnt] = a[i];
                a[i] = t;
                sort(a + cnt + 1, a + len);
                cout << a << endl;
            }
        } 
        return 0;
    }
    
    //next_permutation
    //ref: https://blog.csdn.net/wyg1997/article/details/50973269 
    #include<iostream>
    #include<algorithm> 
    #include<string>
    using namespace std;
    
    int main()
    {
        string str;
        while (cin >> str && str != "#") {
            if (next_permutation(str.begin(), str.end())) {
                cout << str << endl;
            } 
            else cout << "No Successor" << endl;
        } 
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:poj1146 全排列(STL)

          本文链接:https://www.haomeiwen.com/subject/dskebctx.html