結果

問題 No.170 スワップ文字列(Easy)
ユーザー SagTokiSagToki
提出日時 2018-05-30 14:02:49
言語 Java21
(openjdk 21)
結果
AC  
実行時間 122 ms / 5,000 ms
コード長 3,212 bytes
コンパイル時間 7,356 ms
コンパイル使用メモリ 86,896 KB
実行使用メモリ 56,148 KB
最終ジャッジ日時 2023-09-12 20:36:20
合計ジャッジ時間 7,786 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 115 ms
55,472 KB
testcase_01 AC 118 ms
55,592 KB
testcase_02 AC 116 ms
55,820 KB
testcase_03 AC 119 ms
54,476 KB
testcase_04 AC 117 ms
55,532 KB
testcase_05 AC 116 ms
55,652 KB
testcase_06 AC 122 ms
56,148 KB
testcase_07 AC 118 ms
55,820 KB
testcase_08 AC 117 ms
55,584 KB
testcase_09 AC 116 ms
53,908 KB
testcase_10 AC 120 ms
55,904 KB
testcase_11 AC 118 ms
53,504 KB
testcase_12 AC 118 ms
55,620 KB
testcase_13 AC 117 ms
55,492 KB
testcase_14 AC 119 ms
55,752 KB
testcase_15 AC 121 ms
55,652 KB
testcase_16 AC 119 ms
55,812 KB
testcase_17 AC 117 ms
55,560 KB
testcase_18 AC 119 ms
55,824 KB
testcase_19 AC 118 ms
55,580 KB
testcase_20 AC 117 ms
56,060 KB
testcase_21 AC 120 ms
55,668 KB
testcase_22 AC 120 ms
55,532 KB
testcase_23 AC 118 ms
55,756 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

//メインメソッド
public class SwapString {
    public static void main(String[] args){
        ArrayList<Character> Character = Input();
        Collections.sort(Character, Comparator.reverseOrder());
        int len = Character.size();
        //Mapを定義してkeyをアルファベット、valueを0を初期値とする
        Map <Character,Integer> Count = new HashMap<>();
        int ans = 0;
        for(int i = 0 ; i < Character.size() ; i++){
            char now = (char) Character.get(i);
            
            if(Count.containsKey(now)){
                Count.put(now , Count.get(now) + 1);
            }else{
                Count.put(now , 1);
            }
        }
        for(int i = 0 ; i < Character.size() - 1 ;i++){
            if(Objects.equals(Character.get(i), Character.get(i+1))){
               Character.remove(i);
               i--;
            }
        }
        long Answer = 1 ;
        for(int k = 0 ; k < Character.size() ; k++){
            Answer *= Combination(len , Count.get(Character.get(k)));
            len -= Count.get(Character.get(k));
        }
        
        System.out.println(Answer - 1);
    }
    
    //文字列を入力して種類と長さを判定するメソッド
    public static ArrayList<Character> Input(){
        Scanner scanner = new Scanner(System.in);
        String S = scanner.nextLine();
        char[] Character = S.toCharArray();
        try{
            Pattern pattern = Pattern.compile(".*[^A-Z].*");
            Matcher matcher = pattern.matcher(S);
            //文字列に含まれる文字が大文字のアルファベットであるかの判定
            if(matcher.find()){
                System.out.println("文字列は大文字アルファベットで入力してください");
                System.exit(0);
            }
            //文字列の長さが1以上8以下であるかの判定
            if(Character.length < 0 || Character.length > 8){
                System.out.println("文字列は1文字以上8文字以下で入力してください");
                System.exit(0);
            }   
        }catch(Exception E){
            System.out.println("想定外のエラーです");
        }
        //char配列をリストに変換して戻り値にする
        ArrayList<Character> character = new ArrayList<>();
        for(int i = 0 ; i < Character.length ; i++){
            character.add(Character[i]);
        }
        return character;
    }
    
    //コンビネーションの計算を行うメソッド
    public static long Combination(int n , int m){
        if( n < m || m < 0 ) {
           System.out.println( "引数の値が不正です" );
           System.exit(0);
        }
        long Result = 1;
        m = ( n - m < m ? n - m : m );
        for( int ns = n - m + 1, ms = 1; ms <= m; ns ++, ms ++ ) {
            Result *= ns;
            Result /= ms;
        }
        return Result;
    }
}
0