結果

問題 No.52 よくある文字列の問題
ユーザー t8m8⛄️t8m8⛄️
提出日時 2015-04-16 22:18:38
言語 Java21
(openjdk 21)
結果
AC  
実行時間 337 ms / 5,000 ms
コード長 1,676 bytes
コンパイル時間 3,626 ms
コンパイル使用メモリ 78,832 KB
実行使用メモリ 60,696 KB
最終ジャッジ日時 2023-10-22 03:59:00
合計ジャッジ時間 5,504 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 110 ms
57,528 KB
testcase_01 AC 113 ms
57,584 KB
testcase_02 AC 113 ms
57,408 KB
testcase_03 AC 116 ms
57,408 KB
testcase_04 AC 337 ms
60,696 KB
testcase_05 AC 113 ms
57,480 KB
testcase_06 AC 188 ms
58,904 KB
testcase_07 AC 132 ms
58,360 KB
testcase_08 AC 109 ms
57,292 KB
testcase_09 AC 116 ms
57,304 KB
testcase_10 AC 118 ms
57,696 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//No.52 よくある文字列の問題

import java.util.*;
import java.io.*;
import static java.util.Arrays.*;
import static java.lang.Math.*;

public class No52 {
    
    static final Scanner in = new Scanner(System.in);
    static final PrintWriter out = new PrintWriter(System.out,false);

    static void solve() {
        String s = in.next();
        char[] cs = s.toCharArray();
        int cnt = 0;
        sort(cs);
        do {
            String t = String.valueOf(cs);
            if (dfs(t,s,0)) cnt++;
        }while(nextPermutation(cs));
        out.println(cnt);
    }

    static boolean dfs(String s, String t, int idx) {
        char c = s.charAt(idx);
        int len = t.length();
        if (len == 1) return c == t.charAt(0);
        if (t.charAt(0) == c && dfs(s,t.substring(1),idx+1)) return true;
        if (t.charAt(len-1) == c && dfs(s,t.substring(0,len-1),idx+1)) return true;
        return false;
    }

    static boolean nextPermutation(char[] a) {
        int n = a.length, i, j;
        for (i=n-2; i>=0 && a[i]>=a[i+1]; i--);
        if (i == -1) return false;
        for (j=i+1; j<n && a[i]<a[j]; j++);
        char temp = a[i]; a[i] = a[j-1]; a[j-1] = temp;
        for (int l=i+1, r=n-1; l<r; l++,r--) {
            temp = a[l]; a[l] = a[r]; a[r] = temp;
        }
        return true;
    }

    public static void main(String[] args) {
        long start = System.currentTimeMillis();

        solve();
        out.flush();

        long end = System.currentTimeMillis();
        //trace(end-start + "ms");
        in.close();
        out.close();
    }

    static void trace(Object... o) { System.out.println(deepToString(o));}
}
0