結果

問題 No.927 Second Permutation
ユーザー yukileo9
提出日時 2021-07-13 08:40:53
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 872 bytes
コンパイル時間 2,199 ms
コンパイル使用メモリ 170,320 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-02 11:15:35
合計ジャッジ時間 3,139 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 10 WA * 17
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

void no_927(void) {
    string x;
    cin >> x;
    int x_size = (int)x.size();
    sort(x.begin(), x.end());
    reverse(x.begin(), x.end());
    bool can_swap = false;
    if (x[0] == '0') {
        for (int i = 1; i < x_size; i++) {
            if (x[i] != '0') {
                swap(x[0], x[i]);
                can_swap = true;
                break;
            }
        }
        if (!can_swap) {
            cout << "-1" << endl;
            return;
        }
    }
    can_swap = false;
    for (int i = x_size - 2; i >= 1; i--) {
        if (x[x_size - 1] != x[i]) {
            swap(x[x_size - 1], x[i]);
            can_swap = true;
            break;
        }
    }
    if (!can_swap) {
        cout << -1 << endl;
        return;
    }
    cout << x << endl;
}

int main(void) {
    no_927();
    return 0;
}
0