結果

問題 No.927 Second Permutation
ユーザー tsushimatsushima
提出日時 2019-11-23 09:04:49
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 70 ms / 2,000 ms
コード長 1,876 bytes
コンパイル時間 955 ms
コンパイル使用メモリ 116,032 KB
実行使用メモリ 22,528 KB
最終ジャッジ日時 2024-04-19 14:28:54
合計ジャッジ時間 3,334 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
18,560 KB
testcase_01 AC 27 ms
18,688 KB
testcase_02 AC 31 ms
18,688 KB
testcase_03 AC 30 ms
18,816 KB
testcase_04 AC 29 ms
18,816 KB
testcase_05 AC 30 ms
18,688 KB
testcase_06 AC 29 ms
18,560 KB
testcase_07 AC 30 ms
18,432 KB
testcase_08 AC 42 ms
18,688 KB
testcase_09 AC 33 ms
18,432 KB
testcase_10 AC 54 ms
19,200 KB
testcase_11 AC 66 ms
20,096 KB
testcase_12 AC 46 ms
18,688 KB
testcase_13 AC 33 ms
18,412 KB
testcase_14 AC 52 ms
19,328 KB
testcase_15 AC 47 ms
18,944 KB
testcase_16 AC 67 ms
20,352 KB
testcase_17 AC 68 ms
20,352 KB
testcase_18 AC 62 ms
20,096 KB
testcase_19 AC 67 ms
20,224 KB
testcase_20 AC 64 ms
20,352 KB
testcase_21 AC 67 ms
20,352 KB
testcase_22 AC 70 ms
20,224 KB
testcase_23 AC 70 ms
20,352 KB
testcase_24 AC 53 ms
22,528 KB
testcase_25 AC 41 ms
20,864 KB
testcase_26 AC 43 ms
20,352 KB
testcase_27 AC 42 ms
20,096 KB
testcase_28 AC 58 ms
22,400 KB
testcase_29 AC 51 ms
21,632 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Linq;
using System.Collections.Generic;

namespace Algorithm
{
    class Program
    {
        static void Main(string[] args)
        {
            var X = Console.ReadLine().ToCharArray().OrderByDescending(z => z).ToArray();
            var prev = new char[X.Length];
            Array.Copy(X, prev, X.Length);

            var first = true;
            do
            {
                if (first) first = false;
                else
                {
                    var s = new string(X);
                    if (s[0] == '0') break;
                    if (new string(prev) != s)
                    {
                        Console.WriteLine(s);
                        return;
                    }
                }
            } while (PrevPermutation(X));

            Console.WriteLine(-1);
        }

        public static bool PrevPermutation<T>(T[] array, int index, int length, Comparison<T> comp)
        {
            if (length <= 1) return false;
            for (int i = length - 1; i > 0; i--)
            {
                int k = i - 1;
                if (comp(array[k], array[i]) > 0)
                {
                    int j = Array.FindLastIndex(array, delegate (T x) { return comp(array[k], x) > 0; });
                    Swap(ref array[k], ref array[j]);
                    Array.Reverse(array, i, length - i);
                    return true;
                }
            }
            Array.Reverse(array, index, length);
            return false;
        }

        public static bool PrevPermutation<T>(T[] array) where T : IComparable
        {
            return PrevPermutation(array, 0, array.Length, Comparer<T>.Default.Compare);
        }

        public static void Swap<T>(ref T one, ref T two)
        {
            var temp = one;
            one = two;
            two = temp;
        }
    }
}
0