結果

問題 No.927 Second Permutation
ユーザー kpinkcat
提出日時 2023-08-23 14:55:55
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 22 ms / 2,000 ms
コード長 1,174 bytes
コンパイル時間 1,012 ms
コンパイル使用メモリ 109,676 KB
最終ジャッジ日時 2025-02-16 12:32:42
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<map>
#include<vector>
#include <algorithm>
#include<math.h>
#include <iomanip>
#include<set>
#include <numeric>
using namespace std;


int main()
{
    string s, t = "";
    cin >> s;
    int n = s.size(), cnt = 0;
    vector<int> v(10);
    set<int> st;
    bool l = 0, pl = 0;
    for (int i = 0; i < n; i++){
        v[s[i] - '0']++;
    }
    for (int i = 0; i < 10; i++){
        if (v[i]){
            cnt++;
            st.insert(i);
        }
    }
    if (cnt == 1){
        cout << -1 << endl;
        return 0;
    } else if (cnt == 2 && v[0] == n - 1){
        cout << -1 << endl;
        return 0;
    }
    for (int i = 9; i > -1; i--){
        auto itr = st.begin();
        if (i == *st.begin()){
            itr++;
            cout << *itr++;
            v[i]--;
        }
        itr++;
        if (i == *itr) pl = 1;
        while (v[i]){
            if (v[i]!=1){
                cout << to_string(i);
            }
            else if (v[i] == 1 && pl){
                cout << *st.begin();
            } else {
                cout << to_string(i);
            }
            v[i]--;
        }
    }
    cout << t << endl;
}
0