結果

問題 No.927 Second Permutation
ユーザー tsushima
提出日時 2019-11-22 22:14:27
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 42 ms / 2,000 ms
コード長 1,378 bytes
コンパイル時間 893 ms
コンパイル使用メモリ 115,932 KB
実行使用メモリ 20,352 KB
最終ジャッジ日時 2024-10-11 03:55:15
合計ジャッジ時間 3,045 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます
コンパイルメッセージ
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();
            var dict = new Dictionary<char, int>();
            for (var i = 0; i < X.Length; i++)
            {
                if (dict.ContainsKey(X[i]))
                    dict[X[i]]++;
                else
                    dict[X[i]] = 1;
            }

            var count = 0;
            var one = 0;
            var ans = "";
            foreach (var d in dict.OrderBy(z => z.Key))
            {
                if (count == 0)
                    one = d.Value;

                ans = new string(d.Key, d.Value) + ans;
                count++;
            }

            if (X.Length == one)
            {
                Console.WriteLine(-1);
                return;
            }

            var work = ans.ToCharArray();
            Swap(ref work[work.Length-one], ref work[work.Length-one - 1]);
            var last = new string(work);

            if (last[0] == '0' || ans == last)
                Console.WriteLine(-1);
            else
               Console.WriteLine(last);
        }

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