結果

問題 No.927 Second Permutation
ユーザー ei13337
提出日時 2019-11-22 22:44:01
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 1,232 bytes
コンパイル時間 860 ms
コンパイル使用メモリ 86,020 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-10-11 04:21:56
合計ジャッジ時間 1,818 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <iomanip>
#include <algorithm>
using namespace std;
using ll = long long;

int main() {
    string x;
    cin >> x;
    vector<int> c(10, 0);
    for(int i = 0; i < (int)x.size(); i++) {
        c[x[i] - '0']++;
    }
    int n = 0;
    for(int i = 0; i < 10; i++) if(c[i]) n++;
    if(n == 1) {
        cout << -1 << endl;
        return 0;
    }
    n = 0;
    for(int i = 1; i < 10; i++) n += c[i];
    if(n == 1) {
        cout << -1 << endl;
        return 0;
    }
    string ans = ""; bool b = false;
    for(int i = 0; i < 10; i++) {
        if(c[i]) {
            if(!b) {
                for(int j = 0; j < c[i] - 1; j++) ans += '0' + i;
                for(int j = i + 1; j < 10; j++) {
                    if(c[j]) {
                        ans += '0' + j;
                        c[j]--;
                        break;
                    }
                }
                ans += '0' + i;
                b = true;
            } else {
                for(int j = 0; j < c[i]; j++) ans += '0' + i;
            }
        }
    }
    reverse(ans.begin(), ans.end());
    cout << ans << endl;
    return 0;
}
0