結果

問題 No.927 Second Permutation
ユーザー tails1434tails1434
提出日時 2019-12-28 22:23:07
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 70 ms / 2,000 ms
コード長 719 bytes
コンパイル時間 193 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 77,056 KB
最終ジャッジ日時 2024-04-20 19:28:36
合計ジャッジ時間 2,831 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
53,632 KB
testcase_01 AC 41 ms
54,144 KB
testcase_02 AC 42 ms
53,632 KB
testcase_03 AC 42 ms
53,760 KB
testcase_04 AC 42 ms
54,272 KB
testcase_05 AC 42 ms
53,632 KB
testcase_06 AC 41 ms
53,632 KB
testcase_07 AC 41 ms
53,632 KB
testcase_08 AC 54 ms
65,536 KB
testcase_09 AC 45 ms
59,648 KB
testcase_10 AC 56 ms
69,248 KB
testcase_11 AC 59 ms
74,996 KB
testcase_12 AC 54 ms
66,560 KB
testcase_13 AC 48 ms
60,544 KB
testcase_14 AC 55 ms
69,248 KB
testcase_15 AC 52 ms
66,304 KB
testcase_16 AC 64 ms
76,800 KB
testcase_17 AC 65 ms
76,800 KB
testcase_18 AC 67 ms
76,800 KB
testcase_19 AC 70 ms
77,056 KB
testcase_20 AC 67 ms
76,928 KB
testcase_21 AC 64 ms
77,052 KB
testcase_22 AC 68 ms
77,056 KB
testcase_23 AC 67 ms
76,940 KB
testcase_24 AC 45 ms
57,984 KB
testcase_25 AC 44 ms
56,960 KB
testcase_26 AC 44 ms
55,936 KB
testcase_27 AC 45 ms
56,448 KB
testcase_28 AC 48 ms
61,696 KB
testcase_29 AC 47 ms
60,032 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

def main():
    X = input()
    if len(set(X)) == 1:
        print(-1)
        exit()
    if ('0' in set(X)) and (len(X) - X.count('0') <= 1):
        print(-1)
        exit()
    
    d = defaultdict(int)
    for x in X:
        d[x] += 1

    ans = ""
    cnt = 0
    second = -1
    length = len(d)
    for key in sorted(d.keys(), reverse=True):
        if cnt == length - 2:
            ans += key * (d[key] - 1)
            second = key
        elif cnt == length - 1:
            ans += key
            ans += second
            ans += key * (d[key] - 1)
        else:
            ans += key * d[key]
        cnt += 1
    
    print(ans)

if __name__ == "__main__":
    main()
0