結果
問題 |
No.927 Second Permutation
|
ユーザー |
![]() |
提出日時 | 2019-11-23 09:04:49 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 72 ms / 2,000 ms |
コード長 | 1,876 bytes |
コンパイル時間 | 891 ms |
コンパイル使用メモリ | 115,492 KB |
実行使用メモリ | 22,784 KB |
最終ジャッジ日時 | 2024-10-11 06:48:31 |
合計ジャッジ時間 | 3,294 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
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.
ソースコード
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; } } }