結果

問題 No.170 スワップ文字列(Easy)
ユーザー SagTokiSagToki
提出日時 2018-05-30 13:44:22
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 3,117 bytes
コンパイル時間 3,711 ms
コンパイル使用メモリ 85,768 KB
実行使用メモリ 56,548 KB
最終ジャッジ日時 2023-09-12 20:36:35
合計ジャッジ時間 7,413 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 119 ms
55,580 KB
testcase_01 AC 118 ms
55,692 KB
testcase_02 AC 120 ms
55,772 KB
testcase_03 AC 118 ms
56,548 KB
testcase_04 AC 116 ms
55,592 KB
testcase_05 AC 120 ms
55,688 KB
testcase_06 AC 119 ms
56,116 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 116 ms
55,988 KB
testcase_12 AC 116 ms
55,952 KB
testcase_13 AC 118 ms
55,772 KB
testcase_14 AC 118 ms
55,744 KB
testcase_15 WA -
testcase_16 AC 119 ms
55,876 KB
testcase_17 AC 116 ms
55,676 KB
testcase_18 WA -
testcase_19 AC 116 ms
55,652 KB
testcase_20 AC 118 ms
56,192 KB
testcase_21 AC 119 ms
56,208 KB
testcase_22 WA -
testcase_23 AC 118 ms
55,592 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

//メインメソッド
public class SwapString {
    public static void main(String[] args){
        ArrayList Character = Input();
        Collections.reverse(Character);
        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(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