結果

問題 No.39 桁の数字を入れ替え
コンテスト
ユーザー denderaKawazu
提出日時 2018-03-20 23:31:54
言語 Java
(openjdk 25.0.2)
コンパイル:
javac -encoding UTF8 _filename_
実行:
java -ea -Xmx700m -Xss256M -DONLINE_JUDGE=true _class_
結果
AC  
実行時間 58 ms / 5,000 ms
コード長 1,575 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,539 ms
コンパイル使用メモリ 84,908 KB
実行使用メモリ 46,788 KB
最終ジャッジ日時 2026-03-10 21:49:30
合計ジャッジ時間 4,005 ms
ジャッジサーバーID
(参考情報)
judge1_1 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
import java.util.Collections;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Task solver = new Task();
        solver.solve(1, in, out);
        out.close();
    }

    static class Task {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            String s = in.next();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < s.length() - 1; i++) {
                Set<Character> cs = new HashSet<>();
                for (int j = i + 1; j < s.length(); j++) cs.add(s.charAt(j));
                char tc = Collections.max(cs);
                char ic = s.charAt(i);
                if (tc > ic) {
                    int index = s.lastIndexOf(tc);
                    sb.append(s.substring(0, i));
                    sb.append(tc);
                    sb.append(s.substring(i + 1, index));
                    sb.append(ic);
                    sb.append(s.substring(index + 1, s.length()));
                    out.println(sb);
                    return;
                }
            }
            out.println(s);
        }

    }
}

0