結果
問題 |
No.52 よくある文字列の問題
|
ユーザー |
![]() |
提出日時 | 2015-04-16 22:10:49 |
言語 | Java (openjdk 23) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,703 bytes |
コンパイル時間 | 3,355 ms |
コンパイル使用メモリ | 79,516 KB |
実行使用メモリ | 45,960 KB |
最終ジャッジ日時 | 2024-07-04 15:01:43 |
合計ジャッジ時間 | 5,434 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 6 WA * 5 |
ソースコード
//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(); HashSet<String> map = new HashSet<String>(); char[] cs = s.toCharArray(); sort(cs); int cnt = 0; do { if (dfs(s,String.valueOf(cs),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));} }