結果

問題 No.52 よくある文字列の問題
ユーザー ぴろずぴろず
提出日時 2014-12-15 13:07:44
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,327 bytes
コンパイル時間 2,586 ms
コンパイル使用メモリ 82,000 KB
実行使用メモリ 57,432 KB
最終ジャッジ日時 2023-09-02 14:50:48
合計ジャッジ時間 4,669 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 119 ms
55,416 KB
testcase_01 AC 119 ms
55,988 KB
testcase_02 AC 147 ms
55,624 KB
testcase_03 AC 123 ms
55,348 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 122 ms
55,468 KB
testcase_09 AC 115 ms
55,608 KB
testcase_10 AC 116 ms
55,424 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package no052;

import java.util.HashSet;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		char[] s = sc.next().toCharArray();
		int n = s.length;
		String[][] t = new String[(n+1)/2][2];
		for(int i=0;i<n/2;i++) {
			String head = String.valueOf(s[i]);
			String tail = String.valueOf(s[n-1-i]);
			t[i][0] = head + tail;
			t[i][1] = tail + head;
		}
		if (n % 2 == 1) {
			t[n/2][0] = t[n/2][1] = String.valueOf(s[n/2]);
		}
		int m = t.length;
		int[] p = new int[m];
		for(int i=0;i<m;i++) {
			p[i] = i;
		}
		HashSet<String> hs = new HashSet<String>();
		do {
			for(int i=0;i<(1<<m);i++) {
				StringBuilder sb = new StringBuilder();
				for(int j=0;j<m;j++) {
					sb.append(t[p[j]][(i>>j)&1]);
				}
				hs.add(sb.toString());
			}
		} while(nextPermutation(p));
		System.out.println(hs.size());
	}

	static boolean nextPermutation(int[] p) {
		for(int a=p.length-2;a>=0;--a) {
			if(p[a]<p[a+1]) {
				for(int b=p.length-1;;--b) {
					if(p[b]>p[a]) {
						int t = p[a];
						p[a] = p[b];
						p[b] = t;
						for(++a, b=p.length-1;a<b;++a,--b) {
							t = p[a];
							p[a] = p[b];
							p[b] = t;
						}
						return true;
					}
				}
			}
		}
		return false;
	}
}
0